Condividi tramite


Process.SynchronizingObject Proprietà

Definizione

Ottiene o imposta l'oggetto utilizzato per effettuare il marshalling delle chiamate del gestore eventi rilasciate come risultato di un evento di uscita del processo.

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

Valore della proprietà

Oggetto ISynchronizeInvoke utilizzato per effettuare il marshalling delle chiamate del gestore eventi che vengono rilasciate come risultato di un Exited evento nel processo.

Attributi

Esempio

    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

Commenti

Quando SynchronizingObject è null, i metodi che gestiscono l'evento Exited vengono chiamati su un thread dal pool di thread di sistema. Per altre informazioni sui pool di thread di sistema, vedere ThreadPool.

Quando l'evento Exited viene gestito da un componente Visual Windows Form, ad esempio , Buttonl'accesso al componente tramite il pool di thread di sistema potrebbe non funzionare o potrebbe causare un'eccezione. Evitare questo problema impostando SynchronizingObject su un componente Windows Form, che fa sì che i metodi che gestiscono l'evento Exited vengano chiamati nello stesso thread in cui è stato creato il componente.

Se l'oggetto Process viene utilizzato all'interno di Visual Studio 2005 in una finestra di progettazione di Windows Form, SynchronizingObject viene impostato automaticamente sul controllo che contiene l'oggetto Process. Ad esempio, se si inserisce un oggetto Process in una finestra di progettazione per Form1 (che eredita da Form) la SynchronizingObject proprietà di Process viene impostata sull'istanza di Form1:

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

In genere, questa proprietà viene impostata quando il componente viene inserito all'interno di un controllo o di una maschera, perché tali componenti sono associati a un thread specifico.

Si applica a

Vedi anche