This documentation is archived and is not being maintained.

Application.Run Method

Begins running a standard application message loop on the current thread.

Overload List

Begins running a standard application message loop on the current thread, without a form.

[Visual Basic] Overloads Public Shared Sub Run()
[C#] public static void Run();
[C++] public: static void Run();
[JScript] public static function Run();

Begins running a standard application message loop on the current thread, with an ApplicationContext.

[Visual Basic] Overloads Public Shared Sub Run(ApplicationContext)
[C#] public static void Run(ApplicationContext);
[C++] public: static void Run(ApplicationContext*);
[JScript] public static function Run(ApplicationContext);

Begins running a standard application message loop on the current thread, and makes the specified form visible.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Run(Form)
[C#] public static void Run(Form);
[C++] public: static void Run(Form*);
[JScript] public static function Run(Form);

Example

[Visual Basic, C#, C++] The following example lists numbers in a list box on a form. Each time you click button1, the application adds another number to the list.

[Visual Basic, C#, C++] The Main method calls Run to start the application, which creates the form, listBox1, and button1. When the user clicks button1, the button1_Click method adds numbers one to three to the list box, and displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds another number to the list. If the user clicks Yes, the application calls Exit to process all remaining messages in the queue and then to quit.

[Visual Basic, C#, C++] The example assumes that listBox1 and button1 have been created and placed on a form.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Run. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
<STAThread()> _
Shared Sub Main()     
   ' Starts the application.
   Application.Run(New Form1())
End Sub

Sub button1_Click(sender As object, e As System.EventArgs)
   ' Populates a list box with three numbers.
   Dim i As Integer = 3
   Dim j As Integer
   For j = 1 To i - 1
      listBox1.Items.Add(j)
   Next

   ' Checks to see whether the user wants to exit the application.
   ' If not, adds another number to the list box.
   While (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = _ 
      DialogResult.No)
      ' Increments the counter and adds the number to the list box.
      i = i + 1
      listBox1.Items.Add(i)
   End While

   ' The user wants to exit the application. Close everything down.
   Application.Exit()
End Sub


[C#] 
public static void Main(string[] args) {
    // Starts the application.
    Application.Run(new Form1());
 }
 
 protected void button1_Click(object sender, System.EventArgs e) {
    // Populates a list box with three numbers.
    int i = 3;
    for(int j=1; j<=i; j++) {
       listBox1.Items.Add(j);
    }
 
    /* Determines whether the user wants to exit the application.
     * If not, adds another number to the list box. */
    while (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) == 
       DialogResult.No) {
       // Increments the counter ands add the number to the list box.
       i++;
       listBox1.Items.Add(i);
    }
 
    // The user wants to exit the application. Close everything down.
    Application.Exit();
 }


[C++] 
public:
   static void main() {
      // Starts the application.
      Application::Run(new Form1());
   }

protected:
   void button1_Click(Object* sender, System::EventArgs* e) {
      // Populates a list box with three numbers.
      int i = 3;
      for (int j=1; j<=i; j++) {
         listBox1->Items->Add(__box(j));
      }

      /* Determines whether the user wants to exit the application.
      * If not, adds another number to the list box. */
      while (MessageBox::Show(S"Exit application?", S"", MessageBoxButtons::YesNo) ==
         DialogResult::No) {
            // Increments the counter ands add the number to the list box.
            i++;
            listBox1->Items->Add(__box(i));
         }

         // The user wants to exit the application. Close everything down.
         Application::Exit();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Application Class | Application Members | System.Windows.Forms Namespace

Show: