.NET イベント モデルは、あるスレッドでイベントを発生させ、別のスレッドでそれをリッスンすることがよくあります。バックグラウンド スレッドから UI スレッドにイベントをマーシャリングする最もクリーンな方法は何かと考えていました。
コミュニティの提案に基づいて、私はこれを使用しました:
// earlier in the code
mCoolObject.CoolEvent+=
new CoolObjectEventHandler(mCoolObject_CoolEvent);
// then
private void mCoolObject_CoolEvent(object sender, CoolObjectEventArgs args)
{
if (InvokeRequired)
{
CoolObjectEventHandler cb =
new CoolObjectEventHandler(
mCoolObject_CoolEvent);
Invoke(cb, new object[] { sender, args });
return;
}
// do the dirty work of my method here
}