แก้ไข

แชร์ผ่าน


Application.Run Method

Definition

Starts a Windows Presentation Foundation application.

Overloads

Run(Window)

Starts a Windows Presentation Foundation application and opens the specified window.

Run()

Starts a Windows Presentation Foundation application.

Run(Window)

Starts a Windows Presentation Foundation application and opens the specified window.

public:
 int Run(System::Windows::Window ^ window);
[System.Security.SecurityCritical]
public int Run (System.Windows.Window window);
public int Run (System.Windows.Window window);
[<System.Security.SecurityCritical>]
member this.Run : System.Windows.Window -> int
member this.Run : System.Windows.Window -> int
Public Function Run (window As Window) As Integer

Parameters

window
Window

A Window that opens automatically when an application starts.

Returns

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.

Attributes

Exceptions

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

Examples

The following example shows an application with a manually-created static entry point method that instantiates Application, before calling 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()
        {
            Application app = new Application();
            app.Run(new Window());
        }
    }
}

Imports System.Windows

Namespace VisualBasic
    Public Class EntryPoint
        ' All WPF applications should execute on a single-threaded apartment (STA) thread
        <STAThread()>
              Public Shared Sub Main()
            Dim app As New Application()
            app.Run(New Window())
        End Sub
    End Class
End Namespace

Remarks

This overload extends the Run method to open the specified window after an application starts running.

If you define a code Application that opens a window when it starts running, you explicitly call Run.

If you create your Application using markup, or markup and code-behind, you can automatically open a window during using either of the following techniques:

See also

Applies to

Run()

Starts a Windows Presentation Foundation application.

public:
 int Run();
public int Run ();
member this.Run : unit -> int
Public Function Run () As Integer

Returns

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.

Exceptions

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

Examples

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

Imports System.Windows

Namespace VisualBasic
    Public Class EntryPoint
        ' All WPF applications should execute on a single-threaded apartment (STA) thread
        <STAThread()>
              Public Shared Sub Main()
            Dim app As New CustomApplication()
            app.Run()
        End Sub
    End Class

    Public Class CustomApplication
        Inherits Application
        Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
            MyBase.OnStartup(e)

            Dim window As New Window()
            window.Show()
        End Sub
    End Class
End Namespace

Remarks

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.

See also

Applies to