终止已经处于睡眠状态的thred [英] Terminate a thred which is already in sleep

查看:91
本文介绍了终止已经处于睡眠状态的thred的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个后台线程,在DoWork中我需要分两个阶段。所以我在中间使用Thread.Sleep(n)并且一切正常。



但是,现在我需要暂停在DoWork中完成的工作。即使我可以这样做,我也无法终止Thread.Sleep()动作。假设当前线程(在我的情况下为后台线程)设置为休眠10秒,DoWrok在发送取消后10秒左右完成。



我该怎么办?解决这个问题评论真的很感激。

解决方案

你如何解决?不要使用Thread.Sleep!它使线程进入睡眠状态,并且根本不会做任何事情,包括终止!,直到睡眠返回。



因为你不解释你的工作人员线程正在做,为什么你把Thread.Sleep放在那里,几乎不可能给你任何关于你应该做什么的准确答案。


你没有提供代码样品,所以我们无法按照您的预期提供帮助。



我建议您阅读以下内容:

停用和恢复线程 [ ^ ]

线程.Abort方法(对象) [ ^ ]

如何在收到某个答案时停止Thread.Sleep [ ^ ]

在UI上睡觉的危险主题 [ ^ ]

使用线程.Abort() [ ^ ]

Hi all,

I have a background thread and in DoWork I have to some stuff, in two stages. So I use Thread.Sleep(n) in the middle of and everything works fine.

However, now I have a requirement to suspend the work doing in DoWork. Even I can do that I cannot terminate the Thread.Sleep() action. Say the current thread (background thread in my case) set to sleep for 10 seconds, DoWrok complete after 10 seconds or so after I send the cancellation.

How can I resolve this. Comments really appreciate.

解决方案

How do you resolve that?? Don't use Thread.Sleep! It puts the thread to sleep and is won't do anything at all, including terminate!, until the Sleep returns.

Since you don't explain what your worker thread is doing and why you put Thread.Sleep in there, it's pretty much impossible to give you any kind of accurate answer as to what you should be doing instead.


You did not provide code sample, so we can't help as you expect.

I would suggest to read these:
Pausing and Resuming Threads[^]
Thread.Abort Method (Object)[^]
How to stop a Thread.Sleep when a certain answer has been received[^]
The dangers of sleeping on a UI thread[^]
Working with Thread.Abort()[^]


这篇关于终止已经处于睡眠状态的thred的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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