次の方法で共有


Process.SynchronizingObject プロパティ

プロセス終了イベントの結果として発行されるイベント ハンドラ呼び出しをマーシャリングするために使用するオブジェクトを取得または設定します。

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);

プロパティ値

プロセスの Exited イベントの結果として発行されるイベント ハンドラ呼び出しをマーシャリングするために使用する ISynchronizeInvoke

解説

SynchronizingObject が null 参照 (Visual Basic では Nothing) の場合、 Exited イベントを処理するメソッドはシステムのスレッド プールのスレッドで呼び出されます。システム スレッド プールの詳細については、 ThreadPool のトピックを参照してください。

Exited イベントを Button などのビジュアルな Windows フォーム コンポーネントで処理するとき、システム スレッド プールを通じてコンポーネントにアクセスすると、適切に動作しなかったり、例外が発生する場合があります。これを防ぐには、 SynchronizingObject に Windows フォームのコンポーネントを設定して、コンポーネントが作成されているスレッドと同じスレッドで Exited イベントを処理するメソッドが呼び出されるようにします。

Windows フォーム デザイナで Visual Studio .NET の内部で Process を使用すると、 Process を格納しているコントロールが SynchronizingObject に自動的に設定されます。たとえば、 Form から継承される Form1 のデザイナに Process を配置した場合、 ProcessSynchronizingObject プロパティには Form1 のインスタンスが設定されます。

通常、コンポーネントがコントロールまたはフォームの中にあると、これらのコンポーネントは特定のスレッドにバインドされるため、このプロパティが設定されます。

使用例

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

Process クラス | Process メンバ | System.Diagnostics 名前空間