vc中Sleep函数的问题?
我用Sleep做主程序中等,在Sleep前面我开了一个线程。为什么在Sleep的过程中,线程也不干活了??为什么?如果让线程干活,主程序等,用什么方法实现?
问题点数:0、回复次数:6Top
1 楼danscort2000(danscort.yu)回复于 2004-09-03 21:41:23 得分 0
问题不清楚,
是工作者线程还是界面线程?
初始化线程的时候是否挂起还是立即运行?
有没有同步变量控制全局变量?
是否消息死锁?
......Top
2 楼flyelf(空谷清音)回复于 2004-09-03 22:34:25 得分 0
是否在创建线程的时候用了暂停的标志?Top
3 楼mahatma_cn(研究硕士生)回复于 2004-09-03 23:08:21 得分 0
怪问题,没有特殊原因的话,他应该会干活的。Top
4 楼kugou123(酷狗)(彪悍的人生,不需要解释 www.xiaozhou.net)回复于 2004-09-03 23:21:43 得分 0
用WaitForSingleObject();
DWORD WaitForSingleObject(
HANDLE hHandle,
DWORD dwMilliseconds
);
Parameters
hHandle
[in] Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.
If this handle is closed while the wait is still pending, the function's behavior is undefined.
The handle must have the SYNCHRONIZE access right. For more information, see Standard Access Rights.
dwMilliseconds
[in] Time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
Return Values
If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values.
Return Code Description:
WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
WAIT_OBJECT_0 The state of the specified object is signaled.
WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled.
Top
5 楼dansin(平平淡淡才是真)回复于 2004-09-05 10:55:13 得分 0
reference by MSDN..
You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock.Top
6 楼allenq(黑夜给我黑的眼, 我却用他来翻白眼..........)回复于 2004-09-05 12:17:53 得分 0
在主线程中,尽量不要用Sleep去等待啊..
可能就是你的工作线程创建后是挂起状态的,
一般设置一个全局的同步变量, 主线程再另外开启一个线程,去检测这个同步变量的值变化以确定工作线程是否完成
Top




