<CLR C#>
   /// <summary>
   /// MainWindow.xaml の相互作用ロジック
   /// </summary>
   public partial class MainWindow : Window
   {
       public MainWindow()
       {
           InitializeComponent();
       }

       private void ClickButtonCommand(object sender, RoutedEventArgs e)
       {
           var dis = this.Dispatcher;

           //Actionデリゲートに対しラムダ式を利用した方法
           //Action aSyncWork = () =>
           //{
           //    HeavyWork();
           //    dis.Invoke(new Action(() => { this.Button1.IsEnabled = true; }));
           //};

           //スレッド処理のコードはActionやFuncクラスを利用して書くと便利
           //匿名メソッドを利用した方法
           Action aSyncWork = delegate()
           {
               HeavyWork();
               dis.Invoke(new Action(delegate() { this.Button1.IsEnabled = true; }));
               //この匿名メソッドの内容は別スレッドで動くことになるが、ここでは
               //Dispatcherオブジェクトを利用してUIスレッド側に処理を行っている
               //この場合スレッド終了のタイミングでボタンを利用可能にしている
           };

           this.Button1.IsEnabled = false;

           aSyncWork.BeginInvoke(null, null);  //Actionクラスに登録した匿名メソッドをUIスレッドとは別のスレッドで起動して処理
       }
 
       private void HeavyWork()
       {
           System.Threading.Thread.Sleep(3000);
       }
   }
最終更新:2012年11月09日 18:29