/******************************************************************************
Module: SchedLab.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#include "..\CmnHdr.h" /* See Appendix A. */
#include <windowsx.h>
#include <tchar.h>
#include <process.h> // For _beginthreadex
#include "Resource.h"
///////////////////////////////////////////////////////////////////////////////
DWORD WINAPI ThreadFunc(PVOID pvParam)
{
HANDLE hThreadPrimary = (HANDLE) pvParam;
SuspendThread(hThreadPrimary);
chMB(
"The Primary thread is suspended.\n"
"It no longer responds to input and produces no output.\n"
"Press OK to resume the primary thread & exit this secondary thread.\n");
ResumeThread(hThreadPrimary);
CloseHandle(hThreadPrimary); // To avoid deadlock, call EnableWindow after ResumeThread.
EnableWindow(GetDlgItem(FindWindow(NULL, TEXT("Scheduling Lab")), IDC_SUSPEND), TRUE);
return(0);
}以上这段代码是《windows核心编程》第7章SchedLab.cpp 中第1到30行的代码。他为什么说EnableWindow()这个函数能防止死锁?
Module: SchedLab.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#include "..\CmnHdr.h" /* See Appendix A. */
#include <windowsx.h>
#include <tchar.h>
#include <process.h> // For _beginthreadex
#include "Resource.h"
///////////////////////////////////////////////////////////////////////////////
DWORD WINAPI ThreadFunc(PVOID pvParam)
{
HANDLE hThreadPrimary = (HANDLE) pvParam;
SuspendThread(hThreadPrimary);
chMB(
"The Primary thread is suspended.\n"
"It no longer responds to input and produces no output.\n"
"Press OK to resume the primary thread & exit this secondary thread.\n");
ResumeThread(hThreadPrimary);
CloseHandle(hThreadPrimary); // To avoid deadlock, call EnableWindow after ResumeThread.
EnableWindow(GetDlgItem(FindWindow(NULL, TEXT("Scheduling Lab")), IDC_SUSPEND), TRUE);
return(0);
}以上这段代码是《windows核心编程》第7章SchedLab.cpp 中第1到30行的代码。他为什么说EnableWindow()这个函数能防止死锁?
解决方案 »
- 用SetTimer设置同一个nIDEvent多次。在Kill的时候要不要也要Kill同样的次数!
- 先ISpVoice *pVoice = NULL;后pVoice->Release();程序试图释放未被初始化或者非法的内存,问题在哪里呢?见代码。
- 大量图书出让(包括计算机、科普、文学、历史﹑棋书等),有同好者不妨进来看看。
- #include<WinFax.h>????????????
- 关于程序与系统托盘与的交互?
- 对话框做成静态库为什么就弹不出对话框!
- 关于文件读写
- 为什么重载失败?
- 有劳各位,如何学习C++?
- 菜鸟vc++6编译出错,烦请高手解答
- Console下的文件编辑器.
- XML打开与保存
他这么做使得IDC_SUSPEND这个按钮可用,
用户单击这个按钮时,程序可以改变线程的执行情况.
你重点要看IDC_SUSPEND函数里的代码
注:我没看过《windows核心编程>>只是自己猜想的
这里肯定是要激活EnableWindow的控件,那个控件肯定是能让程序正常运行下去的关键,如果退出这个线程
那个控件还没激活就死在那了。
我觉得这里用死锁应该不准确。
在IDC_SUSPEND的处理逻辑中,调用EnableWindow将主窗口——也就是主线程所对应的窗口disable掉了,也就是说这样确保主UI线程从按下IDC_SUSPEND到被挂起之前,用户都不能进行任何操作,确保它在挂起时,是一个干净的状态。