Application.Run Méthode

Définition

Démarre une application Windows Presentation Foundation.

Surcharges

Run()

Démarre une application Windows Presentation Foundation.

Run(Window)

Démarre une application Windows Presentation Foundation et ouvre la fenêtre spécifiée.

Run()

Démarre une application Windows Presentation Foundation.

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

Retours

Code de sortie de l'application Int32 retourné au système d'exploitation lors de la fermeture de l'application. Par défaut, le code de sortie a la valeur 0.

Exceptions

Run() est appelé à partir d’une application hébergée par navigateur (par exemple, une application de navigateur XAML (XBAP)).

Exemples

L’exemple suivant montre une application qui utilise un personnalisé Application et doit donc appeler Runexplicitement .

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

Remarques

Run est appelé pour démarrer une application WPF. Si vous définissez votre Application balisage d’utilisation, ou le balisage et le code-behind, Run sera appelé implicitement. Toutefois, si vous définissez votre Application code à l’aide de , vous devez appeler Runexplicitement .

Quand Run est appelé, Application attache une nouvelle Dispatcher instance au thread d’interface utilisateur. Ensuite, la méthode de Run l’objet Dispatcher est appelée, ce qui démarre une pompe de messages pour traiter les messages Windows. Enfin, l’objet Dispatcher appelle la méthode de l’objet ApplicationOnStartup pour déclencher l’événement Startup . Par conséquent, le modèle d’exécution d’application a été établi au moment où vous gérez Startup, auquel cas l’application est considérée comme en cours d’exécution.

Une application cesse de s’exécuter quand Shutdown est appelée ; la valeur de la ShutdownMode propriété détermine quand Shutdown est appelée et si elle se produit automatiquement ou si vous devez l’appeler explicitement.

Run peut être appelé uniquement à partir du thread qui crée l’objet Application . En outre, Run ne peut pas être appelé à partir d’un XBAP.

Voir aussi

S’applique à

Run(Window)

Démarre une application Windows Presentation Foundation et ouvre la fenêtre spécifiée.

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

Paramètres

window
Window

Window qui s'ouvre automatiquement au démarrage d'une application.

Retours

Code de sortie de l'application Int32 retourné au système d'exploitation lors de la fermeture de l'application. Par défaut, le code de sortie a la valeur 0.

Attributs

Exceptions

Run() est appelé à partir d’une application hébergée par navigateur (par exemple, une application de navigateur XAML (XBAP)).

Exemples

L’exemple suivant montre une application avec une méthode de point d’entrée statique créée manuellement qui instancie Application, avant d’appeler 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

Remarques

Cette surcharge étend la Run méthode pour ouvrir la fenêtre spécifiée après le démarrage de l’exécution d’une application.

Si vous définissez un code Application qui ouvre une fenêtre lorsqu’il commence à s’exécuter, vous appelez Runexplicitement .

Si vous créez votre Application balisage à l’aide, ou le balisage et le code-behind, vous pouvez ouvrir automatiquement une fenêtre lors de l’utilisation de l’une des techniques suivantes :

  • De manière déclarative, en définissant StartupUri.

  • Par programmation, en gérant Startup.

Voir aussi

S’applique à