WinForms でスレッド アクセスをプログラミングしたことはありませんが、PostSharp + Silverlight でプログラミングしました。少しグーグルで検索してみます。ただし、動作することを保証するものではありません!
[Serializable]
public class OnGuiThreadAttribute : MethodInterceptionAspect
{
private static Control MainControl;
//or internal visibility if you prefer
public static void RegisterMainControl(Control mainControl)
{
MainControl = mainControl;
}
public override void OnInvoke(MethodInterceptionArgs eventArgs)
{
if (MainControl.InvokeRequired)
MainControl.BeginInvoke(eventArgs.Proceed);
else
eventArgs.Proceed();
}
}
そして、アイデアはアプリケーションの開始時にあり、メイン/ルートコントロールを属性に登録します。次に、確実にメインスレッドで実行したいメソッドを でデコレートし[OnGuiThread]
ます。すでにメイン スレッド上にある場合は、メソッドを実行するだけです。そうでない場合は、メソッド呼び出しをメイン スレッドへのデリゲートとして非同期的に昇格させます。
編集: 使用しているターゲット コントロールに特定の呼び出しメソッドを使用するように求めていることがわかりました (遅いです)。コントロールのサブクラスでインスタンスメソッドをデコレートすると仮定します。
[Serializable]
public class OnGuiThreadAttribute : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs eventArgs)
{
//you may want to change this line to more gracefully check
//if "Instance" is a Control
Control targetControl = (Control)eventArgs.Instance;
if (targetControl.InvokeRequired)
targetControl.BeginInvoke(eventArgs.Proceed);
else
eventArgs.Proceed();
}
}