线程不能恢复,高分求解!

zhouzhipen 2010-04-15 10:06:49
工作线程内容如下:


CFile file;
if(!file.Open(filename,CFile:modeRead))
return 0;
....
正常的文件读操作,文件非常大所以用线程。
file.Close();



在创建线程后,得到一个线程名句柄 hTr;

用户点暂停时,用SuspendThread把线程挂起。
当恢复时用ResumeThread恢复。

以上操作在XP上完全正常,但在WIN98中却无法恢复线程。难道WIN98中的API 函数ResumeThread会有什么问题?
...全文
150 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzhipen 2010-04-16
  • 打赏
  • 举报
回复
现在终于发现原因了,并不是线程没有被唤醒,而是读操作一直没有返回。
但是新的问题又来了,当我在外部线程中关闭了文件句柄,线程读操作应该是要返回的,在XP下也的确如此,但98中却无法返回。
zhouzhipen 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cnzdgs 的回复:]
最好不要用SuspendThread来暂停线程,线程在运行过程难免要访问共享对象(包括用户态和内核态),通常会通过加锁的方式阻止其它线程访问该对象,此时如果线程被挂起,其它要访问该对象的线程则无法继续执行,可能出现死锁或其它无法预料的结果。
建议改成在线程循环过程中等待一个事件对象,通过设置该事件的状态来控制允许或暂停线程。
[/Quote]

我所提的问题有80%以上都是cnzdgs为我解决的,在此再次感谢!
既然您都认为不要用SuspendThread,我想只有用别的办法了.
iqyely 2010-04-16
  • 打赏
  • 举报
回复
关注下。
mszjk 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cnzdgs 的回复:]
最好不要用SuspendThread来暂停线程,线程在运行过程难免要访问共享对象(包括用户态和内核态),通常会通过加锁的方式阻止其它线程访问该对象,此时如果线程被挂起,其它要访问该对象的线程则无法继续执行,可能出现死锁或其它无法预料的结果。
建议改成在线程循环过程中等待一个事件对象,通过设置该事件的状态来控制允许或暂停线程。
[/Quote]
又是一个抄级专家.
mszjk 2010-04-16
  • 打赏
  • 举报
回复
98跟xp差距太大了....
z8y8x 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zgl7903 的回复:]

文件非常大 建议使用文件映射 映射完毕后就如同对内存指针一样使用
http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
WinXP是基于NT核的 多任务会周全考虑慢速设备
[/Quote]
顶一下
zgl7903 2010-04-16
  • 打赏
  • 举报
回复
文件非常大 建议使用文件映射 映射完毕后就如同对内存指针一样使用
http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
WinXP是基于NT核的 多任务会周全考虑慢速设备
cnzdgs 2010-04-15
  • 打赏
  • 举报
回复
最好不要用SuspendThread来暂停线程,线程在运行过程难免要访问共享对象(包括用户态和内核态),通常会通过加锁的方式阻止其它线程访问该对象,此时如果线程被挂起,其它要访问该对象的线程则无法继续执行,可能出现死锁或其它无法预料的结果。
建议改成在线程循环过程中等待一个事件对象,通过设置该事件的状态来控制允许或暂停线程。
zhouzhipen 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zyyoung 的回复:]
是不是同一台机器
[/Quote]

与机器无关,只要是在XP系统中完成正常。
zhouzhipen 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kier2 的回复:]
ResumeThread
The ResumeThread function decrements a thread's suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed.

DWORD ResumeThread(
HANDLE hThre……
[/Quote]

我刚刚调试了一下,发现线程在一个读取操作还没有完成,这时暂停,无论如何也不能恢复了。
而且 ResumeThread返回1
mcaok 2010-04-15
  • 打赏
  • 举报
回复
不懂帮顶
sxsgf 2010-04-15
  • 打赏
  • 举报
回复
着个网上很多的可以解决
zyyoung 2010-04-15
  • 打赏
  • 举报
回复
是不是同一台机器
kier2 2010-04-15
  • 打赏
  • 举报
回复
ResumeThread
The ResumeThread function decrements a thread's suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed.

DWORD ResumeThread(
HANDLE hThread // handle to thread
);
Parameters
hThread
[in] Handle to the thread to be restarted.
Windows NT/2000/XP: The handle must have THREAD_SUSPEND_RESUME access to the thread. For more information, see Thread Security and Access Rights.

Return Values
If the function succeeds, the return value is the thread's previous suspend count.

If the function fails, the return value is -1. To get extended error information, call GetLastError.

Remarks
The ResumeThread function checks the suspend count of the subject thread. If the suspend count is zero, the thread is not currently suspended. Otherwise, the subject thread's suspend count is decremented. If the resulting value is zero, then the execution of the subject thread is resumed.

If the return value is zero, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted. If the return value is greater than 1, the specified thread is still suspended.

Note that while reporting debug events, all threads within the reporting process are frozen. Debuggers are expected to use the SuspendThread and ResumeThread functions to limit the set of threads that can execute within a process. By suspending all threads in a process except for the one reporting a debug event, it is possible to "single step" a single thread. The other threads are not released by a continue operation if they are suspended.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
看影子win98中应该也可以吧

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧