有两个按钮,一个是挂起线程SuspendThread,一个是唤醒线程ResumeThread,
但挂起线程就出问题,查了MSDN说要为线程设置 THREAD_SUSPEND_RESUME 安全属性,
寻根到了 SECURITY_DESCRIPTOR 结构体,但一查MSDN没有参数,这怎么设置??
哪位牛牛指点指点?
10 个解决方案
#1
If the thread is making a kernel call, SuspendThread fails. An application might need to repeat the SuspendThread several times for it to succeed.
有可能你的线程在调用内核的东西,一般用不着设置安全描述信息的
你看看你的线程有没有其他的问题
lpSecurityDescriptor
A pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process. This is not the same as granting access to everyone by assigning a NULL discretionary access control list (DACL). The default security descriptor is based on the default DACL of the access token belonging to the calling process. By default, the default DACL in the access token of a process allows access only to the user represented by the access token. If other users must access the object, you can either create a security descriptor with the appropriate access, or add ACEs to the DACL that grants access to a group of users.
有可能你的线程在调用内核的东西,一般用不着设置安全描述信息的
你看看你的线程有没有其他的问题
lpSecurityDescriptor
A pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process. This is not the same as granting access to everyone by assigning a NULL discretionary access control list (DACL). The default security descriptor is based on the default DACL of the access token belonging to the calling process. By default, the default DACL in the access token of a process allows access only to the user represented by the access token. If other users must access the object, you can either create a security descriptor with the appropriate access, or add ACEs to the DACL that grants access to a group of users.
#2
你看下这上面的内容全是视频教程很全面,不知道能帮到你不
http://www.abab123.com/bbs/down.asp?html=1656561
http://www.abab123.com/bbs/down.asp?html=1656561
#3
你的线程句柄怎么来的
#4
线程句柄我用成员变量保存了,没有问题,是有效的句柄。
按钮函数里的这句挂起函数的返回值老是 -1, 这个值在MSDN里是说函数调用失败
int val = ::SuspendThread(m_hThread);
m_hThread = (HANDLE)::_beginthreadex(&sec_attr, 0, TMainApp::WorkThreadFunc, 0, NULL, NULL);
#5
最好用EVENT事件对象WaitForSingleObject/SetEvent(),SuspendThread/ResumeThread()并不是太好
#6
SECURITY_DESCRIPTOR简单的传NULL就行了
检查你的句柄是否有效?
m_hThread = (HANDLE)::_beginthreadex(&sec_attr, 0, TMainApp::WorkThreadFunc, 0, NULL, NULL);
检查你的句柄是否有效?
m_hThread = (HANDLE)::_beginthreadex(NULL, 0, &TMainApp::WorkThreadFunc, 0, 0, NULL);
#7
安全属性我传了 NULL,现在是可以挂起了,但不可以恢复了,而且挂起之后,主程序好像也’挂‘了,主程序没有反应,只有用任务管理器强关。
关键代码如下:当线程正在运行时(这个多线程一般运行十几秒),点击but1,挂起线程,显示任务确实暂停了,之后主程序就没反应了,得强关。
void TFrameWnd::TOnBnClicked_but1() // 暂停显示
{
int val = ::SuspendThread(m_hThread); // 这个返回值有效,为 0
char szRes[100];
itoa(val, szRes, 10);
MessageBox(TtheApp.hwndMain, szRes, ("消息"), MB_YESNO); }
void TFrameWnd::TOnBnClicked_but2() // 重置原点
{
int val = ::ResumeThread(m_hThread); // 这个返回值也有效,为0,但没有,而且点一
//次,val的值就加一
char szRes[100];
itoa(val, szRes, 10);
MessageBox(TtheApp.hwndMain, szRes, ("消息"), MB_YESNO);
}
。。。。
m_hThread = (HANDLE)::_beginthreadex(0, 0, TMainApp::WorkThreadFunc, 0, 0, NULL);
。。。。。
unsigned int WINAPI TMainApp::WorkThreadFunc(void* pInfo)
{
TtheApp.theFW->DrawVCodeView(); // 滚动显示任务
::_endthreadex(0);
return 0;
}
#8
MSDN上有句:
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count.
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count.
#9
向请教大牛能就是一下不,不是我看不懂,而是对线程机制有点模糊的理解,
希望能指点一二
#10
每次SuspendThread,线程挂起计数就+1,你必须调用相同次数的ResumeThread,线程才能恢复过来,windows核心编程中有解释
#1
If the thread is making a kernel call, SuspendThread fails. An application might need to repeat the SuspendThread several times for it to succeed.
有可能你的线程在调用内核的东西,一般用不着设置安全描述信息的
你看看你的线程有没有其他的问题
lpSecurityDescriptor
A pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process. This is not the same as granting access to everyone by assigning a NULL discretionary access control list (DACL). The default security descriptor is based on the default DACL of the access token belonging to the calling process. By default, the default DACL in the access token of a process allows access only to the user represented by the access token. If other users must access the object, you can either create a security descriptor with the appropriate access, or add ACEs to the DACL that grants access to a group of users.
有可能你的线程在调用内核的东西,一般用不着设置安全描述信息的
你看看你的线程有没有其他的问题
lpSecurityDescriptor
A pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process. This is not the same as granting access to everyone by assigning a NULL discretionary access control list (DACL). The default security descriptor is based on the default DACL of the access token belonging to the calling process. By default, the default DACL in the access token of a process allows access only to the user represented by the access token. If other users must access the object, you can either create a security descriptor with the appropriate access, or add ACEs to the DACL that grants access to a group of users.
#2
你看下这上面的内容全是视频教程很全面,不知道能帮到你不
http://www.abab123.com/bbs/down.asp?html=1656561
http://www.abab123.com/bbs/down.asp?html=1656561
#3
你的线程句柄怎么来的
#4
线程句柄我用成员变量保存了,没有问题,是有效的句柄。
按钮函数里的这句挂起函数的返回值老是 -1, 这个值在MSDN里是说函数调用失败
int val = ::SuspendThread(m_hThread);
m_hThread = (HANDLE)::_beginthreadex(&sec_attr, 0, TMainApp::WorkThreadFunc, 0, NULL, NULL);
#5
最好用EVENT事件对象WaitForSingleObject/SetEvent(),SuspendThread/ResumeThread()并不是太好
#6
SECURITY_DESCRIPTOR简单的传NULL就行了
检查你的句柄是否有效?
m_hThread = (HANDLE)::_beginthreadex(&sec_attr, 0, TMainApp::WorkThreadFunc, 0, NULL, NULL);
检查你的句柄是否有效?
m_hThread = (HANDLE)::_beginthreadex(NULL, 0, &TMainApp::WorkThreadFunc, 0, 0, NULL);
#7
安全属性我传了 NULL,现在是可以挂起了,但不可以恢复了,而且挂起之后,主程序好像也’挂‘了,主程序没有反应,只有用任务管理器强关。
关键代码如下:当线程正在运行时(这个多线程一般运行十几秒),点击but1,挂起线程,显示任务确实暂停了,之后主程序就没反应了,得强关。
void TFrameWnd::TOnBnClicked_but1() // 暂停显示
{
int val = ::SuspendThread(m_hThread); // 这个返回值有效,为 0
char szRes[100];
itoa(val, szRes, 10);
MessageBox(TtheApp.hwndMain, szRes, ("消息"), MB_YESNO); }
void TFrameWnd::TOnBnClicked_but2() // 重置原点
{
int val = ::ResumeThread(m_hThread); // 这个返回值也有效,为0,但没有,而且点一
//次,val的值就加一
char szRes[100];
itoa(val, szRes, 10);
MessageBox(TtheApp.hwndMain, szRes, ("消息"), MB_YESNO);
}
。。。。
m_hThread = (HANDLE)::_beginthreadex(0, 0, TMainApp::WorkThreadFunc, 0, 0, NULL);
。。。。。
unsigned int WINAPI TMainApp::WorkThreadFunc(void* pInfo)
{
TtheApp.theFW->DrawVCodeView(); // 滚动显示任务
::_endthreadex(0);
return 0;
}
#8
MSDN上有句:
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count.
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count.
#9
向请教大牛能就是一下不,不是我看不懂,而是对线程机制有点模糊的理解,
希望能指点一二
#10
每次SuspendThread,线程挂起计数就+1,你必须调用相同次数的ResumeThread,线程才能恢复过来,windows核心编程中有解释