Partager via


Process.SynchronizingObject Propriété

Définition

Obtient ou définit l’objet utilisé pour marshaler les appels de gestionnaire d’événements émis à la suite d’un événement de sortie de processus.

public:
 property System::ComponentModel::ISynchronizeInvoke ^ SynchronizingObject { System::ComponentModel::ISynchronizeInvoke ^ get(); void set(System::ComponentModel::ISynchronizeInvoke ^ value); };
public System.ComponentModel.ISynchronizeInvoke? SynchronizingObject { get; set; }
public System.ComponentModel.ISynchronizeInvoke SynchronizingObject { get; set; }
[System.ComponentModel.Browsable(false)]
public System.ComponentModel.ISynchronizeInvoke SynchronizingObject { get; set; }
member this.SynchronizingObject : System.ComponentModel.ISynchronizeInvoke with get, set
[<System.ComponentModel.Browsable(false)>]
member this.SynchronizingObject : System.ComponentModel.ISynchronizeInvoke with get, set
Public Property SynchronizingObject As ISynchronizeInvoke

Valeur de propriété

Utilisé ISynchronizeInvoke pour marshaler les appels de gestionnaire d’événements émis à la suite d’un Exited événement sur le processus.

Attributs

Exemples

    private MyButton button1;
    private void button1_Click(object sender, System.EventArgs e)
    {
        using (Process myProcess = new Process())
        {
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("mspaint");
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();
            myProcess.Exited += new EventHandler(MyProcessExited);
            // Set 'EnableRaisingEvents' to true, to raise 'Exited' event when process is terminated.
            myProcess.EnableRaisingEvents = true;
            // Set method handling the exited event to be called  ;
            // on the same thread on which MyButton was created.
            myProcess.SynchronizingObject = button1;
            MessageBox.Show("Waiting for the process 'mspaint' to exit....");
            myProcess.WaitForExit();
        }
    }
    private void MyProcessExited(Object source, EventArgs e)
    {
        MessageBox.Show("The process has exited.");
    }
}

public class MyButton : Button
{
}
    Private button1 As MyButton
    Private Sub button1_Click(sender As Object, e As EventArgs)
        Using myProcess As New Process()
            Dim myProcessStartInfo As New ProcessStartInfo("mspaint")
            myProcess.StartInfo = myProcessStartInfo
            myProcess.Start()
            AddHandler myProcess.Exited, AddressOf MyProcessExited
            ' Set 'EnableRaisingEvents' to true, to raise 'Exited' event when process is terminated.
            myProcess.EnableRaisingEvents = True
            ' Set method handling the exited event to be called  ;
            ' on the same thread on which MyButton was created.
            myProcess.SynchronizingObject = button1
            MessageBox.Show("Waiting for the process 'mspaint' to exit....")
            myProcess.WaitForExit()
        End Using
    End Sub

    Private Sub MyProcessExited(source As Object, e As EventArgs)
        MessageBox.Show("The process has exited.")
    End Sub
End Class

Public Class MyButton
    Inherits Button

End Class

Remarques

Quand SynchronizingObject c’est nullle cas, les méthodes qui gèrent l’événement Exited sont appelées sur un thread à partir du pool de threads système. Pour plus d’informations sur les pools de threads système, consultez ThreadPool.

Lorsque l’événement Exited est géré par un composant Windows Forms visuel, tel qu’un Button, l’accès au composant via le pool de threads système peut ne pas fonctionner ou peut entraîner une exception. Évitez cela en définissant SynchronizingObject sur un composant Windows Forms, ce qui entraîne l’appel des méthodes qui gèrent l’événement Exited sur le même thread sur lequel le composant a été créé.

Si l’élément Process est utilisé dans Visual Studio 2005 dans un concepteur Windows Forms, SynchronizingObject est automatiquement défini sur le contrôle qui contient le Process. Par exemple, si vous placez un concepteur pour Form1 (qui hérite de Form) la SynchronizingObject propriété d’est Process définie sur l’instance de Form1:Process

process1.StartInfo.Domain = "";
process1.StartInfo.LoadUserProfile = false;
process1.StartInfo.Password = null;
process1.StartInfo.StandardErrorEncoding = null;
process1.StartInfo.StandardOutputEncoding = null;
process1.StartInfo.UserName = "";
process1.SynchronizingObject = this;
process1.StartInfo.Domain <- "";
process1.StartInfo.LoadUserProfile <- false;
process1.StartInfo.Password <- null;
process1.StartInfo.StandardErrorEncoding <- null;
process1.StartInfo.StandardOutputEncoding <- null;
process1.StartInfo.UserName <- "";
process1.SynchronizingObject <- this;
process1.StartInfo.Domain = ""
process1.StartInfo.LoadUserProfile = False
process1.StartInfo.Password = Nothing
process1.StartInfo.StandardErrorEncoding = Nothing
process1.StartInfo.StandardOutputEncoding = Nothing
process1.StartInfo.UserName = ""
process1.SynchronizingObject = Me

En règle générale, cette propriété est définie lorsque le composant est placé à l’intérieur d’un contrôle ou d’un formulaire, car ces composants sont liés à un thread spécifique.

S’applique à

Voir aussi