Application Application Application Application Class

Definition

Provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and properties to get information about an application. This class cannot be inherited.

public ref class Application sealed
public sealed class Application
type Application = class
Public NotInheritable Class Application
Inheritance
ApplicationApplicationApplicationApplication

Examples

The following code example lists numbers in a list box on a form. Each time you click button1, the application adds another number to the list.

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 displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds a 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.

Note

The call to Exit will fail in partial trust.

public ref class Form1: public System::Windows::Forms::Form
{
private:
   Button^ button1;
   ListBox^ listBox1;

public:
   Form1()
   {
      button1 = gcnew Button;
      button1->Left = 200;
      button1->Text =  "Exit";
      button1->Click += gcnew EventHandler( this, &Form1::button1_Click );
      listBox1 = gcnew ListBox;
      this->Controls->Add( button1 );
      this->Controls->Add( listBox1 );
   }

private:
   void Form1::button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      int count = 1;
      
      // Check to see whether the user wants to exit 
      // the application. If not, add a number to the list box.
      while ( MessageBox::Show(  "Exit application?",  "", MessageBoxButtons::YesNo ) == ::DialogResult::No )
      {
         listBox1->Items->Add( count );
         count += 1;
      }

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

};

int main()
{
   
   // Starts the application.
   Application::Run( gcnew Form1 );
}

public class Form1 : Form
{
    [STAThread]
    public static void Main()
    {
        // Start the application.
        Application.Run(new Form1());
    }

    private Button button1;
    private ListBox listBox1;

    public Form1()
    {
        button1 = new Button();
        button1.Left = 200;
        button1.Text = "Exit";
        button1.Click += new EventHandler(button1_Click);

        listBox1 = new ListBox();
        this.Controls.Add(button1);
        this.Controls.Add(listBox1);
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        int count = 1;
        // Check to see whether the user wants to exit the application.
        // If not, add a number to the list box.
        while (MessageBox.Show("Exit application?", "", 
            MessageBoxButtons.YesNo)==DialogResult.No)
        {
            listBox1.Items.Add(count);
            count += 1;
        }

        // The user wants to exit the application. 
        // Close everything down.
        Application.Exit();
    }
}
Public Class Form1 
    Inherits Form

    <STAThread()> _
     Shared Sub Main()
        ' Start the application.
        Application.Run(New Form1)
    End Sub

    Private WithEvents button1 As Button
    Private WithEvents listBox1 As ListBox

    Public Sub New()
        button1 = New Button
        button1.Left = 200
        button1.Text = "Exit"

        listBox1 = New ListBox
        Me.Controls.Add(button1)
        Me.Controls.Add(listBox1)
    End Sub

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button1.Click
        Dim count As Integer = 1
        ' Check to see whether the user wants to exit the application.
        ' If not, add a number to the list box.
        While (MessageBox.Show("Exit application?", "", _
            MessageBoxButtons.YesNo) = DialogResult.No)

            listBox1.Items.Add(count)
            count += 1

        End While

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

End Class

Remarks

The Application class has methods to start and stop applications and threads, and to process Windows messages, as follows:

  • Run starts an application message loop on the current thread and, optionally, makes a form visible.

  • Exit or ExitThread stops a message loop.

  • DoEvents processes messages while your program is in a loop.

  • AddMessageFilter adds a message filter to the application message pump to monitor Windows messages.

  • IMessageFilter lets you stop an event from being raised or perform special operations before invoking an event handler.

This class has CurrentCulture and CurrentInputLanguage properties to get or set culture information for the current thread.

You cannot create an instance of this class.

Properties

AllowQuit AllowQuit AllowQuit AllowQuit

Gets a value indicating whether the caller can quit this application.

CommonAppDataPath CommonAppDataPath CommonAppDataPath CommonAppDataPath

Gets the path for the application data that is shared among all users.

CommonAppDataRegistry CommonAppDataRegistry CommonAppDataRegistry CommonAppDataRegistry

Gets the registry key for the application data that is shared among all users.

CompanyName CompanyName CompanyName CompanyName

Gets the company name associated with the application.

CurrentCulture CurrentCulture CurrentCulture CurrentCulture

Gets or sets the culture information for the current thread.

CurrentInputLanguage CurrentInputLanguage CurrentInputLanguage CurrentInputLanguage

Gets or sets the current input language for the current thread.

ExecutablePath ExecutablePath ExecutablePath ExecutablePath

Gets the path for the executable file that started the application, including the executable name.

LocalUserAppDataPath LocalUserAppDataPath LocalUserAppDataPath LocalUserAppDataPath

Gets the path for the application data of a local, non-roaming user.

MessageLoop MessageLoop MessageLoop MessageLoop

Gets a value indicating whether a message loop exists on this thread.

OpenForms OpenForms OpenForms OpenForms

Gets a collection of open forms owned by the application.

ProductName ProductName ProductName ProductName

Gets the product name associated with this application.

ProductVersion ProductVersion ProductVersion ProductVersion

Gets the product version associated with this application.

RenderWithVisualStyles RenderWithVisualStyles RenderWithVisualStyles RenderWithVisualStyles

Gets a value specifying whether the current application is drawing controls with visual styles.

SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat SafeTopLevelCaptionFormat

Gets or sets the format string to apply to top-level window captions when they are displayed with a warning banner.

StartupPath StartupPath StartupPath StartupPath

Gets the path for the executable file that started the application, not including the executable name.

UserAppDataPath UserAppDataPath UserAppDataPath UserAppDataPath

Gets the path for the application data of a user.

UserAppDataRegistry UserAppDataRegistry UserAppDataRegistry UserAppDataRegistry

Gets the registry key for the application data of a user.

UseWaitCursor UseWaitCursor UseWaitCursor UseWaitCursor

Gets or sets whether the wait cursor is used for all open forms of the application.

VisualStyleState VisualStyleState VisualStyleState VisualStyleState

Gets a value that specifies how visual styles are applied to application windows.

Methods

AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter) AddMessageFilter(IMessageFilter)

Adds a message filter to monitor Windows messages as they are routed to their destinations.

DoEvents() DoEvents() DoEvents() DoEvents()

Processes all Windows messages currently in the message queue.

EnableVisualStyles() EnableVisualStyles() EnableVisualStyles() EnableVisualStyles()

Enables visual styles for the application.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Exit() Exit() Exit() Exit()

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs) Exit(CancelEventArgs)

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

ExitThread() ExitThread() ExitThread() ExitThread()

Exits the message loop on the current thread and closes all windows on the thread.

FilterMessage(Message) FilterMessage(Message) FilterMessage(Message) FilterMessage(Message)

Runs any filters against a window message, and returns a copy of the modified message.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OleRequired() OleRequired() OleRequired() OleRequired()

Initializes OLE on the current thread.

OnThreadException(Exception) OnThreadException(Exception) OnThreadException(Exception) OnThreadException(Exception)

Raises the ThreadException event.

RaiseIdle(EventArgs) RaiseIdle(EventArgs) RaiseIdle(EventArgs) RaiseIdle(EventArgs)

Raises the Idle event in hosted scenarios.

RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback) RegisterMessageLoop(Application+MessageLoopCallback)

Registers a callback for checking whether the message loop is running in hosted environments.

RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter) RemoveMessageFilter(IMessageFilter)

Removes a message filter from the message pump of the application.

Restart() Restart() Restart() Restart()

Shuts down the application and starts a new instance immediately.

Run() Run() Run() Run()

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

Run(ApplicationContext) Run(ApplicationContext) Run(ApplicationContext) Run(ApplicationContext)

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

Run(Form) Run(Form) Run(Form) Run(Form)

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

SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean) SetCompatibleTextRenderingDefault(Boolean)

Sets the application-wide default for the UseCompatibleTextRendering property defined on certain controls.

SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean) SetSuspendState(PowerState, Boolean, Boolean)

Suspends or hibernates the system, or requests that the system be suspended or hibernated.

SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode) SetUnhandledExceptionMode(UnhandledExceptionMode)

Instructs the application how to respond to unhandled exceptions.

SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

Instructs the application how to respond to unhandled exceptions, optionally applying thread-specific behavior.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
UnregisterMessageLoop() UnregisterMessageLoop() UnregisterMessageLoop() UnregisterMessageLoop()

Unregisters the message loop callback made with RegisterMessageLoop(Application+MessageLoopCallback).

Events

ApplicationExit ApplicationExit ApplicationExit ApplicationExit

Occurs when the application is about to shut down.

EnterThreadModal EnterThreadModal EnterThreadModal EnterThreadModal

Occurs when the application is about to enter a modal state.

Idle Idle Idle Idle

Occurs when the application finishes processing and is about to enter the idle state.

LeaveThreadModal LeaveThreadModal LeaveThreadModal LeaveThreadModal

Occurs when the application is about to leave a modal state.

ThreadException ThreadException ThreadException ThreadException

Occurs when an untrapped thread exception is thrown.

ThreadExit ThreadExit ThreadExit ThreadExit

Occurs when a thread is about to shut down. When the main thread for an application is about to be shut down, this event is raised first, followed by an ApplicationExit event.

Applies to