Partage via


Application.Run Méthode

Définition

Démarre une application Windows Presentation Foundation.

Surcharges

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.

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

Une Window qui s’ouvre automatiquement lorsqu’une application démarre.

Retours

Code de sortie de l’application Int32 retourné au système d’exploitation lorsque l’application s’arrête. Par défaut, la valeur du code de sortie est 0.

Attributs

Exceptions

Run() est appelé à partir d’une application hébergée par un 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 méthode Run pour ouvrir la fenêtre spécifiée après l’exécution d’une application.

Si vous définissez un code Application qui ouvre une fenêtre au démarrage de l’exécution, vous appelez explicitement Run.

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

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

  • Par programmation, en gérant Startup.

Voir aussi

S’applique à

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 lorsque l’application s’arrête. Par défaut, la valeur du code de sortie est 0.

Exceptions

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

Exemples

L’exemple suivant montre une application qui utilise un Application personnalisé et doit donc appeler explicitement 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

Remarques

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

Quand Run est appelée, Application attache une nouvelle instance de Dispatcher au thread d’interface utilisateur. Ensuite, la méthode Run de 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 Application de l’objet OnStartup pour déclencher l’événement de Startup. Par conséquent, le modèle d’exécution de l’application a été établi au moment où vous gérez Startup, à quel moment 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 propriété ShutdownMode détermine quand Shutdown est appelée et si elle se produit automatiquement ou si vous devez l’appeler explicitement.

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

Voir aussi

S’applique à