15 个解决方案
#1
使用CreateEvent创建HANDLE,重载主程序的OnDestroy,在此写SetEvent;同时在线程中发现HANDLE有变化,则在线程中调用ExitThread等结束线程。
#2
具体一点好吗?
#3
还有其他的方法吗?
#4
用全局变量吧
volatile bool bRun=false;
启动线程的时候:
bRun=tre;
启动线程
主程序结束的时候:
bRun=false;
在线程中:
while(bRun)
{
//您的其他工作
}
volatile bool bRun=false;
启动线程的时候:
bRun=tre;
启动线程
主程序结束的时候:
bRun=false;
在线程中:
while(bRun)
{
//您的其他工作
}
#5
1,创建线程时,用一全局变量存放线程句柄;
HANDLE m_hTemp = CreateThread(...);
2,需退出时;
DWORD dwThreadExitCode;
GetExitCodeThread(m_hTemp, &dwThreadExitCode);
TerminateThread(m_hTemp, dwThreadExitCode);
HANDLE m_hTemp = CreateThread(...);
2,需退出时;
DWORD dwThreadExitCode;
GetExitCodeThread(m_hTemp, &dwThreadExitCode);
TerminateThread(m_hTemp, dwThreadExitCode);
#6
以上是最好的方法,但是我在用的时候没有关闭线程。
#7
你的线程中要有接收消息的动作就行了,下面的代码是masterz大侠写的
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
就是关键
The PostThreadMessage function posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
BOOL PostThreadMessage(
DWORD idThread, // thread identifier
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}
unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
就是关键
The PostThreadMessage function posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
BOOL PostThreadMessage(
DWORD idThread, // thread identifier
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}
unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
#8
进程退出时,由它所创建的所有线程都将会被关闭,但是可能会有部分在线程中取得的资源无法释放,可以通过信号量的方式,在创建线程时CreateSemaphore,
等待线程结束可以用WaitSingleObject或WaitMultiObject
等待线程结束可以用WaitSingleObject或WaitMultiObject
#9
调用ExitThread函数来中止线程
#10
线程句柄本身就是一个信号.
WaitForSingleObject或WaitForMultiObjects是一定要用的,将线程句柄加入等待的信号中,在需要关闭线程时,直接CloseHandle(hThread)就会触发Wait函数.
至于Wait函数的处理不用说了吧!
WaitForSingleObject或WaitForMultiObjects是一定要用的,将线程句柄加入等待的信号中,在需要关闭线程时,直接CloseHandle(hThread)就会触发Wait函数.
至于Wait函数的处理不用说了吧!
#11
先终止线程,再删除线程指针(为防止内存溢出)。
线程终止自己有两条途径
1。当线程函数返回时,线程终止。
2。调用AfxEdnThread
如果设置CWinThread类的m_bAutoDelete设为FALSE,则一旦觉察到他的相关线程消亡,则CWinThread类的对象可自我删除。
如果设为TRUE,则要手工删除。可在使用WaitSingleObject(pThread->m_hThread,INFINITE)后,delete pThread.
当主窗口关闭后,可采用全局变量通知线程函数,使其终止,参见iicup(双杯献酒) 。
线程终止自己有两条途径
1。当线程函数返回时,线程终止。
2。调用AfxEdnThread
如果设置CWinThread类的m_bAutoDelete设为FALSE,则一旦觉察到他的相关线程消亡,则CWinThread类的对象可自我删除。
如果设为TRUE,则要手工删除。可在使用WaitSingleObject(pThread->m_hThread,INFINITE)后,delete pThread.
当主窗口关闭后,可采用全局变量通知线程函数,使其终止,参见iicup(双杯献酒) 。
#12
up
#13
如果你的线程里面的函数发生阻塞,那么除了TerminateThread之外是没有办法的。
#14
TerminateThread
最危险和最简单的办法
最危险和最简单的办法
#15
TerminateThread
最危险和最简单的办法 -- 危险是指什么,能具体点吗?
最危险和最简单的办法 -- 危险是指什么,能具体点吗?
#1
使用CreateEvent创建HANDLE,重载主程序的OnDestroy,在此写SetEvent;同时在线程中发现HANDLE有变化,则在线程中调用ExitThread等结束线程。
#2
具体一点好吗?
#3
还有其他的方法吗?
#4
用全局变量吧
volatile bool bRun=false;
启动线程的时候:
bRun=tre;
启动线程
主程序结束的时候:
bRun=false;
在线程中:
while(bRun)
{
//您的其他工作
}
volatile bool bRun=false;
启动线程的时候:
bRun=tre;
启动线程
主程序结束的时候:
bRun=false;
在线程中:
while(bRun)
{
//您的其他工作
}
#5
1,创建线程时,用一全局变量存放线程句柄;
HANDLE m_hTemp = CreateThread(...);
2,需退出时;
DWORD dwThreadExitCode;
GetExitCodeThread(m_hTemp, &dwThreadExitCode);
TerminateThread(m_hTemp, dwThreadExitCode);
HANDLE m_hTemp = CreateThread(...);
2,需退出时;
DWORD dwThreadExitCode;
GetExitCodeThread(m_hTemp, &dwThreadExitCode);
TerminateThread(m_hTemp, dwThreadExitCode);
#6
以上是最好的方法,但是我在用的时候没有关闭线程。
#7
你的线程中要有接收消息的动作就行了,下面的代码是masterz大侠写的
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
就是关键
The PostThreadMessage function posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
BOOL PostThreadMessage(
DWORD idThread, // thread identifier
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}
unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
就是关键
The PostThreadMessage function posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
BOOL PostThreadMessage(
DWORD idThread, // thread identifier
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}
unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
#8
进程退出时,由它所创建的所有线程都将会被关闭,但是可能会有部分在线程中取得的资源无法释放,可以通过信号量的方式,在创建线程时CreateSemaphore,
等待线程结束可以用WaitSingleObject或WaitMultiObject
等待线程结束可以用WaitSingleObject或WaitMultiObject
#9
调用ExitThread函数来中止线程
#10
线程句柄本身就是一个信号.
WaitForSingleObject或WaitForMultiObjects是一定要用的,将线程句柄加入等待的信号中,在需要关闭线程时,直接CloseHandle(hThread)就会触发Wait函数.
至于Wait函数的处理不用说了吧!
WaitForSingleObject或WaitForMultiObjects是一定要用的,将线程句柄加入等待的信号中,在需要关闭线程时,直接CloseHandle(hThread)就会触发Wait函数.
至于Wait函数的处理不用说了吧!
#11
先终止线程,再删除线程指针(为防止内存溢出)。
线程终止自己有两条途径
1。当线程函数返回时,线程终止。
2。调用AfxEdnThread
如果设置CWinThread类的m_bAutoDelete设为FALSE,则一旦觉察到他的相关线程消亡,则CWinThread类的对象可自我删除。
如果设为TRUE,则要手工删除。可在使用WaitSingleObject(pThread->m_hThread,INFINITE)后,delete pThread.
当主窗口关闭后,可采用全局变量通知线程函数,使其终止,参见iicup(双杯献酒) 。
线程终止自己有两条途径
1。当线程函数返回时,线程终止。
2。调用AfxEdnThread
如果设置CWinThread类的m_bAutoDelete设为FALSE,则一旦觉察到他的相关线程消亡,则CWinThread类的对象可自我删除。
如果设为TRUE,则要手工删除。可在使用WaitSingleObject(pThread->m_hThread,INFINITE)后,delete pThread.
当主窗口关闭后,可采用全局变量通知线程函数,使其终止,参见iicup(双杯献酒) 。
#12
up
#13
如果你的线程里面的函数发生阻塞,那么除了TerminateThread之外是没有办法的。
#14
TerminateThread
最危险和最简单的办法
最危险和最简单的办法
#15
TerminateThread
最危险和最简单的办法 -- 危险是指什么,能具体点吗?
最危险和最简单的办法 -- 危险是指什么,能具体点吗?