英語で読む

次の方法で共有


Process.SynchronizingObject プロパティ

定義

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

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

プロパティ値

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

属性

    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
{
}

注釈

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

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

Processが visual Studio 2005 内でWindows フォーム デザイナーで使用されている場合、 SynchronizingObject は を含むコントロールに自動的に設定されますProcess。 たとえば、 (をForm継承する) のForm1デザイナーに を配置Processすると、 SynchronizingObjectProcess プロパティは の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;

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

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください