Example #1
0
        /// <summary>フォームロードのUOCメソッド</summary>
        protected override void UOC_FormInit()
        {
            int wait = 1;
            int max = 5;

            MyBaseAsyncFunc af = new MyBaseAsyncFunc(this);

            // 非同期処理本体
            af.AsyncFunc = delegate(object param)
            {
                for (this.Current = 1; this.Current <= max; this.Current++)
                {
                    // ダミー
                    System.Threading.Thread.Sleep(wait * 1000);

                    // 進捗表示
                    af.ExecChangeProgress(string.Format(
                        "処理中です・・・:{0}/{1}", this.Current.ToString(), max.ToString()));
                }

                return "処理が完了しました。";
            };

            // 進捗報告・無名関数デレゲード
            af.ChangeProgress = delegate(object param)
            {
                this.label1.Text = (string)param;
            };

            // 結果設定・無名関数デレゲード
            af.SetResult = delegate(object retVal)
            {
                if (retVal is Exception)
                {
                    // 例外発生時
                    this.label1.Text = (retVal as Exception).ToString();
                }
                else
                {
                    this.label1.Text = (string)retVal;
                    this.btnStart.Visible = true;
                }
            };

            if (af.Start())
            {
                //正常に実行
                this.label1.Text = string.Format(
                    "処理中です・・・:{0}/{1}", this.Current.ToString(), max.ToString());
            }
            else
            {
                // ここは通らないが念のため
                this.label1.Text = string.Format(
                    "非同期スレッドが最大数に達しています。:{0}", BaseAsyncFunc.ThreadCount.ToString());
            }
        }
Example #2
0
        /// <summary>非同期実行</summary>
        private void UOC_btnASync_Click(RcFxEventArgs rcFxEventArgs)
        {
            int wait = (int)this.numericUpDown1.Value;

            MyBaseAsyncFunc af = new MyBaseAsyncFunc(this);
            //MyBaseAsyncFunc af = new MyBaseAsyncFunc(this.panel1);

            // 非同期処理本体・無名関数デレゲード
            af.AsyncFunc = delegate(object param)
            {
                // 進捗報告
                af.ExecChangeProgress(string.Format("スレッド実行中: {0}秒待つ", wait));

                System.Threading.Thread.Sleep(wait * 1000);

                return "終わり";
            };

            // 進捗報告・無名関数デレゲード
            af.ChangeProgress = delegate(object param)
            {
                string text = (string)param;
                this.AddStatus(text);
            };

            // 結果設定・無名関数デレゲード
            af.SetResult = delegate(object retVal)
            {
                if (retVal is Exception)
                {
                    // 例外発生時
                    Exception ex = (Exception)retVal;
                    this.AddStatus(string.Format("スレッド実行終了: エラー発生:{0}", ex.Message));
                }
                else
                {
                    this.AddStatus("スレッド実行終了");
                    //throw new Exception("SetResultでエラーとなった場合。");
                }

                // 結果表示のテスト
                this.TestOfResultDisplay();

                // フォーカス制御をする場合、
                this.BeginInvoke(new MethodInvoker(this.SetForcus));

            };

            // 非同期処理を開始させる。
            if (af.Start())
            {
                this.AddStatus(string.Format(
                    "キューイングされました、現在のスレッド数:{0}",
                    BaseAsyncFunc.ThreadCount.ToString()));
            }
            else
            {
                this.AddStatus(string.Format(
                    "非同期スレッドが最大数に達しています。:{0}",
                    BaseAsyncFunc.ThreadCount.ToString()));
            }
        }
Example #3
0
        /// <summary>件数取得</summary>
        /// <param name="rcFxEventArgs">イベントハンドラの共通引数</param>
        /// <remarks>
        /// 非同期フレームワークを使用してB層の呼び出し処理を非同期化
        /// (非同期実行、結果表示の双方に匿名デリゲードを使用するパターン)
        /// </remarks>
        protected void UOC_btnButton1_Click(RcFxEventArgs rcFxEventArgs)
        {
            // 非同期処理クラスを生成
            // 匿名デリゲードの場合は、ベース2で良い。
            MyBaseAsyncFunc af = new MyBaseAsyncFunc(this);

            // 引数を纏める
            af.Parameter = (object)new TestParameterValue(
                this.Name, rcFxEventArgs.ControlName, "SelectCount",
                ((ComboBoxItem)this.ddlDap.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlMode1.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlMode2.SelectedItem).Value + "%"
                + ((ComboBoxItem)this.ddlExRollback.SelectedItem).Value,
                MyBaseControllerWin.UserInfo);

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

                // 戻り値
                TestReturnValue testReturnValue;

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

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

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

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

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

                // 結果表示
                return testReturnValue;
            };

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

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

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

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

            // 非同期実行する。
            if (!af.Start())
            {
                MessageBox.Show("別の非同期処理が実行中です。");
            }
        }
Example #4
0
        /// <summary>隠しボタンのイベント実装</summary>
        protected void UOC_btnHdnBtn1_Click(RcFxEventArgs rcFxEventArgs)
        {
            MessageBox.Show("UOC_btnHdnBtn1_Click");

            if (cbxDoClick2.Checked && txtStatus.Text.Length < 500)
            {
                //// 反転
                //cbxDoClick2.Checked = !cbxDoClick2.Checked;

                int wait = (int)this.numericUpDown1.Value;

                //MyBaseAsyncFunc af = new MyBaseAsyncFunc(this);
                MyBaseAsyncFunc af = new MyBaseAsyncFunc(this.panel1);

                // 非同期処理本体・無名関数デレゲード
                af.AsyncFunc = delegate(object param)
                {
                    // 進捗報告
                    af.ExecChangeProgress(string.Format("スレッド実行中: {0}秒待つ", wait));

                    System.Threading.Thread.Sleep(wait * 1000);

                    return "終わり";
                };

                // 進捗報告・無名関数デレゲード
                af.ChangeProgress = delegate(object param)
                {
                    string text = (string)param;
                    this.AddStatus(text);
                };

                // 結果設定・無名関数デレゲード
                af.SetResult = delegate(object retVal)
                {
                    if (retVal is Exception)
                    {
                        // 例外発生時
                        Exception ex = (Exception)retVal;
                        this.AddStatus(string.Format("スレッド実行終了: エラー発生:{0}", ex.Message));
                    }
                    else
                    {
                        this.AddStatus("スレッド実行終了");
                    }

                    // 結果表示のテスト
                    this.TestOfResultDisplay();
                };

                // 非同期処理を開始させる。
                if (af.Start())
                {
                    this.AddStatus(string.Format(
                        "キューイングされました、現在のスレッド数:{0}",
                        BaseAsyncFunc.ThreadCount.ToString()));
                }
                else
                {
                    this.AddStatus(string.Format(
                        "非同期スレッドが最大数に達しています。:{0}",
                        BaseAsyncFunc.ThreadCount.ToString()));
                }
            }
        }