Application.Run Method

Starts a Windows Presentation Foundation (WPF) application.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public int Run()
You cannot use methods in XAML.

Return Value

Type: System.Int32
The Int32 application exit code that is returned to the operating system when the application shuts down. By default, the exit code value is 0.

ExceptionCondition
InvalidOperationException

Run is called from a browser-hosted application (for example, an XAML browser application (XBAP)).

Run is called to start a WPF application. If you define your Application using markup, or markup and code-behind, Run will be called implicitly. However, if you define your Application using code, you will need to explicitly call Run.

When Run is called, Application attaches a new Dispatcher instance to the UI thread. Next, the Dispatcher object's Run method is called, which starts a message pump to process windows messages. Finally, the Dispatcher object calls the Application object's the OnStartup method to raise the Startup event. Consequently, the application execution model has been established by the time you handle Startup, at which point the application is considered to be running.

An application stops running when Shutdown is called; the value of the ShutdownMode property determines when Shutdown is called, and whether it happens automatically or you need to explicitly call it.

Run can be called only from the thread that creates the Application object. Also, Run cannot be called from a XBAP.

The following example shows an application that uses a custom Application and must therefore explicitly call Run.

using System;
using System.Windows;

namespace CSharp
{
    public class EntryPoint
    {
        // All WPF applications should execute on a single-threaded apartment (STA) thread
        [STAThread]
        public static void Main()
        {
            CustomApplication app = new CustomApplication();
            app.Run();
        }
    }

    public class CustomApplication : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Window window = new Window();
            window.Show();
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft