Process.SynchronizingObject 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置用于封送由于进程退出事件而发出的事件处理程序调用的对象。
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
属性值
ISynchronizeInvoke,它用于封送由于进程上的 Exited 事件而发出的事件处理程序调用。
- 属性
示例
ref class MyButton: public Button
{
public:
void MyProcessExited( Object^ source, EventArgs^ e )
{
MessageBox::Show( "The process has exited." );
}
};
public:
MyButton^ button1;
private:
void MyProcessExited( Object^ source, EventArgs^ e )
{
MessageBox::Show( "The process has exited." );
}
void button1_Click( Object^ sender, EventArgs^ e )
{
Process^ myProcess = gcnew Process;
ProcessStartInfo^ myProcessStartInfo = gcnew ProcessStartInfo( "mspaint" );
myProcess->StartInfo = myProcessStartInfo;
myProcess->Start();
myProcess->Exited += gcnew System::EventHandler( this, &Form1::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();
}
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
注解
当 为 null
时SynchronizingObject,将在系统线程池的Exited线程上调用处理事件的方法。 有关系统线程池的详细信息,请参阅 ThreadPool。
Exited当事件由视觉对象Windows 窗体组件(如 Button)处理时,通过系统线程池访问该组件可能不起作用,或者可能导致异常。 通过将 设置为 SynchronizingObject Windows 窗体 组件来避免这种情况,这会导致在创建组件的同一线程上调用处理Exited事件的方法。
Process如果在 Windows 窗体 设计器的 Visual Studio 2005 中使用 , SynchronizingObject 将自动设置为包含 的Process控件。 例如,如果将 (的设计器上放置Process继承自 Form) SynchronizingObject 的 属性Process设置为 的Form1
实例:Form1
this->process1->StartInfo->Domain = "";
this->process1->StartInfo->LoadUserProfile = false;
this->process1->StartInfo->Password = nullptr;
this->process1->StartInfo->StandardErrorEncoding = nullptr;
this->process1->StartInfo->StandardOutputEncoding = nullptr;
this->process1->StartInfo->UserName = "";
this->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
通常,当组件放置在控件或窗体内时设置此属性,因为这些组件绑定到特定线程。