使用Backgroundworker cancleAsync() [英] using Backgroundworker cancleAsync()

查看:63
本文介绍了使用Backgroundworker cancleAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

i use this code:

private void BW_DoWork(object sender, DoWorkEventArgs e)
       {
       
           if (BW.CancellationPending)
           {
               e.Cancel = true;
               return;
           }
               MainPage MP = new MainPage();
               MP.thisconnection.Open();
               MP.ShowDialog();

       }


但取消按钮无效.


but canclation button don''t work.
How i can cancle operation before it''s complete?

推荐答案

设置cancel属性不会执行任何操作,除非您的线程也检查了该属性并决定停止处理.这是一种发送消息的方式,而不是一种无论线程在做什么都杀死它的方法(此时已完成了一半).这使您可以处理正在处理的对象并执行任何清理代码.

更具体地说,您需要按Cancel(取消)线程启动的时刻,此代码才能起作用,因为否则,MainPage将显示,并且无法再次访问事件args.您要为progress事件添加一个处理程序,并定期调用它,然后在此处检查此属性.
Setting the cancel property does nothing, unless your thread also checks the property and decides to stop processing. It''s a way of sending a message, not a way to kill a thread no matter what it''s doing ( and has half done at that point ). this allows you to deal with the object you''re working on and perform any clean up code.

To be more specific, you need to press cancel the MOMENT your thread starts, for this code to work, because otherwise, MainPage is showing, and does not have access to the event args, again. You want to add a handler for the progress event, and call it regularly, then it is where you check this property.



请参考以下链接:
BackgroundWorker线程和支持取消 [面向初学者的BackgroundWorker类示例 [ [
Hi,
Refer the links below:
BackgroundWorker Threads and Supporting Cancel[^]
BackgroundWorker Class Sample for Beginners[^]

Check this[^] also...


--Amit


这篇关于使用Backgroundworker cancleAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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