如何在backgroundworker C#中关闭messagebox,winform [英] How to close messagebox in backgroundworker C#, winform

查看:148
本文介绍了如何在backgroundworker C#中关闭messagebox,winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我开发了关于backgroundworker和progressbar控件的示例项目。

当前,我遇到了问题,

如何关闭MessageBox(aaaacascscascsacascsa这是测试版)在BackgroundWorker中?



如果我取消线程后台工作,那么消息框仍会显示。

这是我的全部代码:



Hi everybody!
I develop sample project about backgroundworker and progressbar control.
Current, I meet a problem,
How to closed "MessageBox(aaaacascscascsacascsa this is tesst)" in BackgroundWorker?

If i cancellation thread backgroundworker then messagebox still display.
This is all my code:

public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        int _max = 1000;
        private void btnStart_Click(object sender, EventArgs e)
        {
            //Execute th background worker doWork()
            this.bgWorker.RunWorkerAsync();
            this.pgbar.Maximum = this._max;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //request cancellation of bgWorker
            this.bgWorker.CancelAsync();
        }

        //Display
        public void display(string text)
        {
            MessageBox.Show(text);
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //There should be no GUI component method
            for (int i = 0; i <= _max; i++)
            {
                //set cancellation to pending. Execute cancellation
                if (this.bgWorker.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                {
                    MessageBox.Show("aaaacascscascsacascsa this is tesst");
                    this.simulateHeavyWork();
                    this.bgWorker.ReportProgress(i);
                }
            }
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //this is updated from doWork. Its where GUI components are updated
            //revices updates after 100 ms
            this.pgbar.Value = e.ProgressPercentage;
            this.label1.Text = e.ProgressPercentage.ToString() + "%";
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //called when the heavy operation in bg in ower.Can also accept GUI component
            if (e.Cancelled)
            {
                display("Work cancelled!");
            }
            else
            {
                display("Work completed successfully!");
            }
        }

        //simulate complex calculations- time let refresh
        private void simulateHeavyWork()
        {
            Thread.Sleep(100);
        }
    }





我使用C#,winform。

任何想法,我非常感激!

非常感谢你!



我尝试了什么:



网址参考:

http://stackoverflow.com/questions/10498555/calling-showdialog-in-backgroundworker

http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread



I using C#, winform.
Any ideas, I am very grateful!
thank you very much!

What I have tried:

URL reference:
http://stackoverflow.com/questions/10498555/calling-showdialog-in-backgroundworker
http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread

推荐答案

MessageBox.Show 阻止当前线程,直到用户关闭该消息。工作线程在被阻止时无法处理取消请求。



从工作线程显示模态消息也可能不是一个好主意。它可能会产生意想不到的后果:

MessageBoxes和工作线程 [ ^ ]



你在做什么似乎是一个非常糟糕的主意。您真的希望用户必须单击确定1000次吗?
MessageBox.Show blocks the current thread until the message is closed by the user. The worker thread cannot process the cancellation request whilst it is blocked.

It's also probably not a good idea to show a modal message from a worker thread. It could have unintended consequences:
MessageBoxes and worker threads[^]

What you're doing seems like a very bad idea. Do you really want your users to have to click "OK" 1000 times?


这篇关于如何在backgroundworker C#中关闭messagebox,winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆