0 out of 2 rated this helpful Rate this topic

Application Class

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.

System.Object
  System.Windows.Forms.Application

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public sealed class Application

The Application type exposes the following members.

  Name Description
Public property Static member AllowQuit Gets a value indicating whether the caller can quit this application.
Public property Static member CommonAppDataPath Gets the path for the application data that is shared among all users.
Public property Static member CommonAppDataRegistry Gets the registry key for the application data that is shared among all users.
Public property Static member CompanyName Gets the company name associated with the application.
Public property Static member CurrentCulture Gets or sets the culture information for the current thread.
Public property Static member CurrentInputLanguage Gets or sets the current input language for the current thread.
Public property Static member ExecutablePath Gets the path for the executable file that started the application, including the executable name.
Public property Static member LocalUserAppDataPath Gets the path for the application data of a local, non-roaming user.
Public property Static member MessageLoop Gets a value indicating whether a message loop exists on this thread.
Public property Static member OpenForms Gets a collection of open forms owned by the application.
Public property Static member ProductName Gets the product name associated with this application.
Public property Static member ProductVersion Gets the product version associated with this application.
Public property Static member RenderWithVisualStyles Gets a value specifying whether the current application is drawing controls with visual styles.
Public property Static member SafeTopLevelCaptionFormat Gets or sets the format string to apply to top-level window captions when they are displayed with a warning banner.
Public property Static member StartupPath Gets the path for the executable file that started the application, not including the executable name.
Public property Static member UserAppDataPath Gets the path for the application data of a user.
Public property Static member UserAppDataRegistry Gets the registry key for the application data of a user.
Public property Static member UseWaitCursor Gets or sets whether the wait cursor is used for all open forms of the application.
Public property Static member VisualStyleState Gets a value that specifies how visual styles are applied to application windows.
Top
  Name Description
Public method Static member AddMessageFilter Adds a message filter to monitor Windows messages as they are routed to their destinations.
Public method Static member DoEvents Processes all Windows messages currently in the message queue.
Public method Static member EnableVisualStyles Enables visual styles for the application.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Static member Exit Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Public method Static member Exit(CancelEventArgs) Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Public method Static member ExitThread Exits the message loop on the current thread and closes all windows on the thread.
Public method Static member FilterMessage Runs any filters against a window message, and returns a copy of the modified message.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member OleRequired Initializes OLE on the current thread.
Public method Static member OnThreadException Raises the ThreadException event.
Public method Static member RaiseIdle Raises the Idle event in hosted scenarios.
Public method Static member RegisterMessageLoop Registers a callback for checking whether the message loop is running in hosted environments.
Public method Static member RemoveMessageFilter Removes a message filter from the message pump of the application.
Public method Static member Restart Shuts down the application and starts a new instance immediately.
Public method Static member Run Begins running a standard application message loop on the current thread, without a form.
Public method Static member Run(ApplicationContext) Begins running a standard application message loop on the current thread, with an ApplicationContext.
Public method Static member Run(Form) Begins running a standard application message loop on the current thread, and makes the specified form visible.
Public method Static member SetCompatibleTextRenderingDefault Sets the application-wide default for the UseCompatibleTextRendering property defined on certain controls.
Public method Static member SetSuspendState Suspends or hibernates the system, or requests that the system be suspended or hibernated.
Public method Static member SetUnhandledExceptionMode(UnhandledExceptionMode) Instructs the application how to respond to unhandled exceptions.
Public method Static member SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean) Instructs the application how to respond to unhandled exceptions, optionally applying thread-specific behavior.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Static member UnregisterMessageLoop Unregisters the message loop callback made with RegisterMessageLoop.
Top
  Name Description
Public event Static member ApplicationExit Occurs when the application is about to shut down.
Public event Static member EnterThreadModal Occurs when the application is about to enter a modal state.
Public event Static member Idle Occurs when the application finishes processing and is about to enter the idle state.
Public event Static member LeaveThreadModal Occurs when the application is about to leave a modal state.
Public event Static member ThreadException Occurs when an untrapped thread exception is thrown.
Public event Static member 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.
Top

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.

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 Note

The call to Exit will fail in partial trust.


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();
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Choose all that apply about the Application class.

A.  All of its members are static.

B.  It provides methods to start, stop, or filter Windows messages.

C.  The Run method starts the application thread's message loop.

D.  The Run method has two signatures: the first signature involves no parameters, which are normally used for non-GUI applications; the second signature takes a form as a parameter.

E.  You can create an instance of this class.

Answer: ???