Exemplo n.º 1
0
        /// <summary>件数取得</summary>
        /// <remarks>
        /// 非同期フレームワークを使用してB層の呼び出し処理を非同期化
        /// (非同期実行、結果表示の双方に匿名デリゲードを使用するパターン)
        /// </remarks>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // 非同期処理クラスを生成
            // 匿名デリゲードの場合は、ベース2で良い。
            MyBaseAsyncFunc af = new MyBaseAsyncFunc(this);

            // 引数を纏め非同期処理クラスに設定
            af.Parameter = (object)new TestParameterValue(
                this.Name, ((Button)sender).Name, "SelectCount",
                ((ComboBoxItem)this.ddlDap.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlMode1.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlMode2.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlExRollback.SelectedItem).Value,
                this.myUserInfo);

            // 画面上のデータは退避する
            //(オブジェクトであれば、クローンする。)
            string logicalName = ((ComboBoxItem)this.ddlTransmission.SelectedItem).Value;

            // 非同期実行するメソッドを指定(匿名デリゲード)
            // ここは副スレッドから実行されるので注意
            // (画面上のメンバに触らないこと!)。
            af.AsyncFunc = delegate(object param)
            {
                // 引数クラス(キャスト)
                TestParameterValue testParameterValue = (TestParameterValue)param;

                // 戻り値
                TestReturnValue testReturnValue;

                // 呼出し制御部品(スレッドセーフでないため副スレッド内で作る)
                CallController callCtrl = new CallController("");

                // Invoke
                testReturnValue = (TestReturnValue)callCtrl.Invoke(
                    logicalName, testParameterValue);

                //// 進捗表示のテスト
                //af.ChangeProgress = delegate(object o)
                //{
                //    MessageBox.Show(o.ToString());
                //};

                //af.ExecChangeProgress("進捗表示");

                //// 非同期メッセージボックス表示のテスト
                //MessageBoxResult mr = af.ShowAsyncMessageBoxWPF(
                //    "メッセージ", "タイトル", MessageBoxButton.YesNo, MessageBoxImage.Information);
                //// 非同期メッセージボックス表示のテスト(エラー)
                //System.Windows.Forms.DialogResult dr = af.ShowAsyncMessageBoxWin("メッセージ", "タイトル",
                //    System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Information);

                // 結果表示
                return testReturnValue;
            };

            // 結果表示のメソッドを指定(匿名デリゲード)
            // このメソッドは必ず主スレッドで実行される。
            // (画面上のメンバを更新できる!)。
            af.SetResult = delegate(object retVal)
            {
                if (retVal is Exception)
                {
                    // 例外発生時
                    MessageBox.Show(retVal.ToString(), "非同期処理で例外発生!");
                }
                else
                {
                    // 正常時

                    // 戻り値(キャスト)
                    TestReturnValue testReturnValue = (TestReturnValue)retVal;

                    // 結果表示するメッセージ エリア
                    this.labelMessage.Content = "";

                    if (testReturnValue.ErrorFlag == true)
                    {
                        // 結果(業務続行可能なエラー)
                        this.labelMessage.Content = "ErrorMessageID:" + testReturnValue.ErrorMessageID + "\r\n";
                        this.labelMessage.Content += "ErrorMessage:" + testReturnValue.ErrorMessage + "\r\n";
                        this.labelMessage.Content += "ErrorInfo:" + testReturnValue.ErrorInfo + "\r\n";
                    }
                    else
                    {
                        // 結果(正常系)
                        this.labelMessage.Content = testReturnValue.Obj.ToString() + "件のデータがあります";
                    }
                }
            };

            // 非同期実行する。
            if (!af.StartByThreadPool())
            {
                MessageBox.Show("別の非同期処理が実行中です。");
            }
        }