0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[C#] 実行処理中にはボタンを非活性にすると、親切だなって思う

Posted at

処理に時間がかかるものなら、処理中にアプリ側で実行中なのか分かりづらい。
ボタンを非活性にすると、処理中は「ボタン押せないから処理実行中なんだな」とわかる。

            //ボタンを非活性にする
            btn.Enabled = false;
            
            //ボタンを活性にする
            btn.Enabled = true;

こういう実行処理がある。

            try
            {
                //実行
                Controller.実行処理();

                //処理完了
                Controller.OutPutLog($"処理が完了しました  時刻:{DateTime.Now}");
                MessageBox.Show("処理が完了しました", "完了", MessageBoxButtons.OK,MessageBoxIcon.None);
            }
            catch (Exception ex)//エラー時
            {
                Controller.OutPutLog($"エラーが発生しました 時刻:{DateTime.Now}\r\n{ex.Message}");
                MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

ボタン活性、非活性を入れ込んでみた。
活性処理をfinallyに入れてあげることで、正常、エラー時、のどちらも対応できる

            //ボタンを非活性にする
            btn.Enabled = false;
            
            try
            {
                //実行
                Controller.実行処理();

                //処理完了
                Controller.OutPutLog($"処理が完了しました  時刻:{DateTime.Now}");
                MessageBox.Show("処理が完了しました", "完了", MessageBoxButtons.OK,MessageBoxIcon.None);
            }
            catch (Exception ex)//エラー時
            {
                Controller.OutPutLog($"エラーが発生しました 時刻:{DateTime.Now}\r\n{ex.Message}");
                MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //ボタンを活性にする
                btn.Enabled = true;
            }
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?