关于backgroundworker的取消 void bw_DoWork(object sender, DoWorkEventArgs e) { this.Downloader.RunThread(worker); }当用户取消时,如何中止这个下载线程啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请求取消backgroundWorker控件挂机的操作就可以了。bw.CancelAsync(); 最好判断一下: if (backgroundWorker1.IsBusy) { backgroundWorker1.CancelAsync(); } void bw_DoWork(object sender, DoWorkEventArgs e) { if (worker.CancellationPending) { e.Cancel = true; } else { this.Downloader.RunThread(worker); } } 点取消按钮的事件执行 bgWorker.CancelAsync(); 你这个也不行啊。dowork一直检测这个CancellationPending吗,即使为真,也只是把e.Cancel = true,一旦runthread运行,也没把他停止啊 在bw_DoWork里执行的代码本来就在工作线程里,你不需要再启动一个线程,把线程里面的代码移出来。 当用户取消时候,得写个事件,让这个事件去判断,bg是否busy,是就cancel掉。 你的程序代码是否放在worker的另一个线程中运行了this.Downloader.RunThread(worker); 参考下backgroundworker的用法,需要把运行的代码放在dowork中的,而不是另外开个线程。 //纯手写的,没有在ide中测试过,仅供参考private Thread currentThread=null;void DoWork(object sender, DoWorkEventArgs e){ //获取工作者的线程 currentThread=Thread.CurrentThread; //DoSomeWork();}//调用这个方法来立即终止workpublic void Cancel(){if(currentThread!=null){currentThread.Abort();currentThread=null;}} 求助!!DataGridView中内容的更新 100分问个ProgressBar显示百分比进度条 JAVA的问题 打开工程问题 webbrowser滚动条问题 问题:在WORD中替换文本,遇见错误,请大虾执教(在线等) C#操作LPT口发送打印机指令 请问在C#里面怎样和DCOM交互? 怎样获得服务器的时间并用农历显示出来? 麻烦大家,delegate在C#中到底是起什么用的?请知道的朋友详细讲解一下或者来几个基础性的事例! 通过身份证获取信息 .net mvc 框架中jquery方法未能正确执行的问题
bw.CancelAsync();
{
backgroundWorker1.CancelAsync();
}
{
if (worker.CancellationPending)
{
e.Cancel = true;
}
else
{
this.Downloader.RunThread(worker);
}
}
this.Downloader.RunThread(worker); 参考下backgroundworker的用法,需要把运行的代码放在dowork中的,而不是另外开个线程。
void DoWork(object sender, DoWorkEventArgs e)
{
//获取工作者的线程
currentThread=Thread.CurrentThread;
//DoSomeWork();
}
//调用这个方法来立即终止work
public void Cancel()
{
if(currentThread!=null)
{
currentThread.Abort();
currentThread=null;
}
}