如何中止线程? [英] How to abort a thread?

查看:74
本文介绍了如何中止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在vb.net 2.0中开发的winform应用程序,它通过HTTP请求调用webservices并通过解析它来显示HTTP响应xml。现在根据响应xml中的参数登录时,我必须及时调用webservice我正在通过线程实现它。在登录时,如果参数响应xml,则会创建一个后台线程,它会一直以特定的间隔调用webservice。问题是当用户点击注销然后我需要销毁该线程。我无法弄清楚如何销毁该线程,因为调用Abort方法会导致异常。请帮助。

I have a winform application developed in vb.net 2.0 which calls webservices through HTTP request and displays the HTTP response xml by parsing it.Now while logging in based on a parameter in response xml I have to call a webservice at a timely interval.I am achieving it by threading.During logging in if the parameter comes in response xml an background thread is created which keeps calling the webservice at particular interval.The problem is when the user clicks on logout then i need to destroy that thread.I am not being able to figure out how to destroy that thread as calling Abort method would lead to exception.Please help.

推荐答案

这样做的标准方法是有一个标志变量,你定期检查线程是否应该停止处理。如果您的进程可能正在休眠或忙于处理,我发现有一种方法可以通知线程它应该停止处理。



这样说,我更倾向于使用像Task Parallel Library这样的东西来处理线程。使用TPL,我只使用CancellationToken。
The standard way of doing this is to have a flag variable that you periodically check to see if the thread should stop processing. In the case where your process might be sleeping or busy processing, I find it helpful to have a method to signal the thread that it should stop processing.

Saying that, I tend to prefer using something like the Task Parallel Library to handle threading. With TPL, I would just use a CancellationToken.


这篇关于如何中止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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