首页 > 编程语言 >C#中使用BackgroundWorker控件

C#中使用BackgroundWorker控件

时间:2022-09-29 10:57:10浏览次数:60  
标签:控件 C# listBoxConnect BackgroundWorker DateTime Add ToString Items Now

在C#中,BackgroundWorker控件允许在单独的专用线程上运行操作。 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面(UI)似乎处于停止响应状态。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题。

以下代码有部分是项目中的业务代码,请选择性忽略

程序的执行步骤如下:

1.开始执行异步线程,进行后台操作,给后台传递参数

 public void Connect()
        {
            if (this.backgroundWorker1.IsBusy)
                return;
            listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Start connection:Device connection in progress");
            //  RequestResult res = outService.PostConnect(new RequestConnect { }).Result;
            listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Start connection:Device connected successfully");
            if (true)
                this.backgroundWorker1.RunWorkerAsync(10);  //开启线程
        }

 //执行线程任务
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = this.ConnectWorker(this.backgroundWorker1, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

2.进行后台任务执行,并获得任务结果

 private bool ConnectWorker(BackgroundWorker backgroundWorker, DoWorkEventArgs e)
        {
            listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Start connection:Secs service starting");
            if (_secsGem == null)
            {
                SECSConnect();
                Thread.Sleep(3000);
            }
            listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Start connection:MES connection in progress");
            int RepeatTimes = (int)e.Argument;
            for (int i = 0; i < RepeatTimes; i++)
            {
                if (backgroundWorker.CancellationPending)
                    return false;
                if (bussService.SendS1F1().Code == "0")
                    return true;
                else
                {
                    listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Failed to send for the " + (i + 1) + " times");
                    Thread.Sleep(1000);
                }
            }
            return false;
        }

3.结束任务

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (!(bool)e.Result)
                //{
                //    labelStatus.Text = StatusEnum.ONLINE.ToString();
                //    listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Connection success");
                //}
                //else
                {
                    labelStatus.Text = StatusEnum.OFFLINE.ToString();
                    listBoxConnect.Items.Add(DateTime.Now.ToString() + "   Connection failed,Transfer to OFFLINE state");
                }
                listBoxConnect.Update();
            }

            catch (TargetInvocationException ex)
            {
                MessageBox.Show(ex.InnerException.GetType().ToString());
            }
        }

该控件也可与进度条控件等结合使用

 

参考文档:https://www.jb51.net/article/238684.htm

                 https://blog.csdn.net/u012563853/article/details/124042188

标签:控件,C#,listBoxConnect,BackgroundWorker,DateTime,Add,ToString,Items,Now
From: https://www.cnblogs.com/M-MAKI/p/16740683.html

相关文章