Form does not displayed as Dialog inside backgroundworker?

人走茶凉 提交于 2019-12-24 14:31:54

问题


the Form does not displayed as Dialog inside backgroundworker?

Here is my code:

//From Form1
        private void backgroundWorkerResult_DoWork(object sender, DoWorkEventArgs e)
    {
        //do something here

        backgroundWorkerResult.ReportProgress(100);

        frmMessageBox frmMsgBox = new frmMessageBox();
        frmMsgBox.ShowDialog();
    }

Even though i already showed the frmMsgBox as a dialog i Can still click the Form1 that supposed to be not? how can fix this?


回答1:


I created a simple code sample that you can use to understand how the background worker works. Copy the code to a test form and add the following controls

  1. Label Control - Name it 'lblStatus'
  2. Progressbar Control - It should be named progressBar1.
  3. Add 2 buttons named 'btnStartAsyncOperation' and 'btnCancel' and link their click events.

Basically you should display the results in the RunWorkerCompleted event

    public partial class Form1 : Form
    {
        BackgroundWorker backgroundWorker;

        public Form1()
        {
            InitializeComponent();
            backgroundWorker = new BackgroundWorker {WorkerReportsProgress = true, WorkerSupportsCancellation = true};

            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);

        }

        void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                lblStatus.Text = "Task Cancelled.";
            else if (e.Error != null)
                lblStatus.Text = "Error - " + e.Error.Message;
            else
                lblStatus.Text = "Task Completed...";

            btnStartAsyncOperation.Enabled = true;
            btnCancel.Enabled = false;
        }

        void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }

        void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(100);
                backgroundWorker.ReportProgress(i);
                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    backgroundWorker.ReportProgress(0);
                    return;
                }
            }
            backgroundWorker.ReportProgress(100);
        }

        private void btnStartAsyncOperation_Click(object sender, EventArgs e)
        {
            btnStartAsyncOperation.Enabled = false;
            btnCancel.Enabled = true;
            backgroundWorker.RunWorkerAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                backgroundWorker.CancelAsync();
            }
        }
    }

Edited as per comment by the OP

In the DoWork event, set your string as follows

 e.Result = "Your String";

and in the RunWorkerCompleted event

string muResult = Convert.ToString(e.Result);

NOTE: e.Result is an object so you can even set your custom classes with multiple properties in it.



来源:https://stackoverflow.com/questions/16115980/form-does-not-displayed-as-dialog-inside-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!