64,670
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <Windows.h>
#include <stdio.h>
using namespace std;
DWORD WINAPI ThreadFunc(LPVOID lParam)
{
cout<<"线程开始运行"<<endl;
while(true)
{
cout<<"线程运行中.."<<endl;
}
return 0;
}
int main()
{
//创建线程
DWORD threadId;
HANDLE hTread = CreateThread(NULL,0,ThreadFunc,NULL,0,&threadId);
//判断是否按下了ESC,如果按下,终止线程
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD irInBuf[1];
DWORD cNumRead;
while(true)
{
ReadConsoleInput(hStdin,irInBuf,1,&cNumRead);
if(irInBuf[0].Event.KeyEvent.bKeyDown && irInBuf[0].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
{
cout<<"按下了ESC"<<endl;
TerminateThread(hTread,0);
break;
}
}
cout<<"线程已结束"<<endl;
//.....
return 0;
}
int main()
{
//创建线程
DWORD threadId;
HANDLE hTread = CreateThread(NULL,0,ThreadFunc,NULL,0,&threadId);
//判断是否按下了ESC,如果按下,终止线程
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD irInBuf[1];
DWORD cNumRead;
while(true)
{
ReadConsoleInput(hStdin,irInBuf,1,&cNumRead);
if(irInBuf[0].Event.KeyEvent.bKeyDown && irInBuf[0].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
{
cout<<"按下了ESC"<<endl;
// TerminateThread(hTread,0);
CloseHandle(hTread);
break;
}
}
cout<<"线程已结束"<<endl;
return 0;
}