[C#新手入门 四] Winform 多线程初步 BackgroundWorker的用法

时间:2021-08-31 20:07:23

DataGridView App using BackgroundWorker for Async Data Load

http://code.msdn.microsoft.com/DataGridView-App-using-605c1697/view/SourceCode#content

 

backgroundworker使用 实现进度条ProgressBar

http://hi.baidu.com/server126/item/a2508600129959d71ef046d6

 

DataGrid using BackgroundWorker - C#

http://www.mindstick.com/Articles/7f2eeb0d-de48-4496-91c1-4b68a466f4c4/?DataGrid%20using%20BackgroundWorker%20-%20C

 

C#中BackgroundWorker中的DoWork执行了多次

http://www.crifan.com/csharp_backgroundworker_dowork_called_multiple_time/

 

backgroundworker类使用心得

http://www.cnblogs.com/cykevin/articles/1229012.html

 

深度剖析BackgroudWorker类

http://blog.sina.com.cn/s/blog_6fd674050100se72.html

 

在UI程序设计中使用BackgroudWorker进行多线程异步处理

http://blog.sina.com.cn/s/blog_4853e71c0100k0by.html

 

 

 

报错:此 BackgroundWorker 声明它不报告进度。请修改 WorkerReportsProgress 以声明它报告进度。

backgroundWorker2.WorkerReportsProgress=True

 

 

报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务


CancelAsync之后,BackgroundWorker.CancellationPending = true
你要自己处理,具体如下:

private bool DownLoad(BackgroundWorker worker, DoWorkEventArgs e, string[] pramlist)
{
for (int i = 0; i < 100; i++)
{
if(worker.CancellationPending)
{
return false;
}
System.Threading.Thread.Sleep(1000);

worker.ReportProgress(i, "下载开始");
}

return true;
}


 


其次Begin加IsBusy判断

private void buttonBegin_Click(object sender, EventArgs e)
{
if(backgroundWorker1.IsBusy)
{
return;
}
string[] pramlist = { "0"};

backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.RunWorkerAsync(pramlist);
}