Export (0) Print
Expand All

Process.SynchronizingObject Property

Gets or sets the object used to marshal the event handler calls that are issued as a result of a process exit event.

[Visual Basic]
Public Property SynchronizingObject As ISynchronizeInvoke
[C#]
public ISynchronizeInvoke SynchronizingObject {get; set;}
[C++]
public: __property ISynchronizeInvoke* get_SynchronizingObject();
public: __property void set_SynchronizingObject(ISynchronizeInvoke*);
[JScript]
public function get SynchronizingObject() : ISynchronizeInvoke;
public function set SynchronizingObject(ISynchronizeInvoke);

Property Value

The ISynchronizeInvoke used to marshal event handler calls that are issued as a result of an Exited event on the process.

Remarks

When SynchronizingObject is a null reference (Nothing in Visual Basic), methods handling the Exited event are called on a thread from the system thread pool. For more information on system thread pools, see ThreadPool.

When the Exited event is handled by a visual Windows Forms component, such as a Button, accessing the component through the system thread pool might not work, or might result in an exception. Avoid this by setting SynchronizingObject to a Windows Forms component, which causes the methods handling the Exited event to be called on the same thread on which the component was created.

If the Process is used inside Visual Studio .NET in a Windows Forms designer, SynchronizingObject is automatically set to the control containing the Process. For example, if you place a Process on a designer for Form1 (which inherits from Form) the SynchronizingObject property of Process is set to the instance of Form1.

Typically, this property is set when the component is placed inside a control or form, because those components are bound to a specific thread.

Example

[Visual Basic] 
Private button1 As MyButton
Private Sub button1_Click(sender As Object, e As EventArgs)
   Dim myProcess As New Process()
   Dim myProcessStartInfo As New ProcessStartInfo("mspaint")
   myProcess.StartInfo = myProcessStartInfo
   myProcess.Start()
   AddHandler myProcess.Exited, AddressOf button1.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()
   myProcess.Close()
End Sub 'button1_Click
   End Class 'Form1

   Public Class MyButton
Inherits Button
Public Sub MyProcessExited(source As Object, e As EventArgs)
   MessageBox.Show("The process has exited.")
End Sub 'MyProcessExited
   End Class 'MyButton

[C#] 
private MyButton button1;
private void button1_Click(object sender, System.EventArgs e)
{
   Process myProcess = new Process();
   ProcessStartInfo myProcessStartInfo= new ProcessStartInfo("mspaint");
   myProcess.StartInfo = myProcessStartInfo;
   myProcess.Start();
   myProcess.Exited += new EventHandler(button1.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();
   myProcess.Close();
}
   }

   public class MyButton:Button
   {
public void MyProcessExited(Object source, EventArgs e)
{
   MessageBox.Show("The process has exited.");
}
   }

[C++] 
__gc class MyButton : public Button {
public:
    void MyProcessExited(Object* source, EventArgs* e) {
        MessageBox::Show(S"The process has exited.");
    }
};

MyButton* button1;

    void button1_Click(Object* sender, EventArgs* e) {
    Process* myProcess = new Process();
    ProcessStartInfo* myProcessStartInfo = new ProcessStartInfo(S"mspaint");
    myProcess->StartInfo = myProcessStartInfo;
    myProcess->Start();
    myProcess->Exited += new EventHandler(button1, &MyButton::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(S"Waiting for the process 'mspaint' to exit....");
    myProcess->WaitForExit();
    myProcess->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Process Class | Process Members | System.Diagnostics Namespace

Show:
© 2014 Microsoft