如何安全地打破线程 [英] How to safely break the thread

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

问题描述



i有Windows Form Class,名为Download。

此类的Form Load事件引发新线程开始下载新的

文件。 www。

问题是当我要终止表格时。

我还需要打破线程执行,并关闭所有流(响应

流和文件流)

如何以专业的方式做到这一点。

非常感谢

解决方案

< blockquote>这真的很简单,谢谢。


如果我试图访问例如附在表格上的标签怎么样。


您的代码:

void DownloadThreadProc(object obj)

{

byte [] buffer;

while(!this.Disposing)

{

buffer = downloadFile(amountOfBytes);

storeDownloadedBytes(buffer);

//在这里访问标签

}

//这里的清理行动

//关闭连接,释放资源。 />
}

问题是我可以检查标志,但是当

表格已经处理时,该标志将不存在。

因此我不能交流ces要么是旗帜,要么是标签。

换句话说,形式可能会被放置在while循环中,我不会

能够访问旗帜。

PK


这就是我需要的男人

你真的帮了我

PK

Hi
i have Windows Form Class called Download.
Form Load event of this class raises new thread that starts downloading new
file via. www.
Problem is when i want to terminate the form.
I also need to break thread execution, and close all streams ( response
stream and file stream )
How to do this in professional way.
Many thanks

解决方案

It''s really simple, thanks.


What about if i''ll try to access for example Label attached to the form.

Your code:
void DownloadThreadProc(object obj)
{
byte[] buffer;
while ( !this.Disposing)
{
buffer = downloadFile(amountOfBytes);
storeDownloadedBytes(buffer);
//access label here
}
//cleanup actions here
//closing connection, freeing resource.
}
Problem is that i can check the flag, but the flag will not exist when the
form is already disposed.
Thus i cannot acces either flag, label.
In other words form might be disposed inside the while loop, and i will not
be able to access the flag.
PK


That is what i needed man
You really helped me
PK


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

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