背景:以前的正常跑着的系统,由于新需求需要在主进程里新建一个线程用于将某窗体置顶
采用<thread.h>这个库进行编辑多进程,结果发现
“__uncaught_exception”: 找不到标识符
查了网上的一些教程,发现并没有完美的解决办法,考虑没有添加<thread.h>时代码能够正常编译,因此测试了一下,注释掉<thread.h>编译通过,果断放弃这个类,采用了windows自带的API
添加进程方法
DWORD WINAPI TopMost(LPVOID lpParamter)
{
HWND hMis;
do
{
hMis = FindWindow(NULL, L"APP_SOFTPOS");
} while (hMis==NULL);
SetWindowPos(hMis,HWND_TOP,0,0,0,0,SWP_NOACTIVATE);//窗口置顶 SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE
return 0L;
}
执行进程
HANDLE hThread = CreateThread(NULL, 0, TopMost, NULL, 0, NULL);
CloseHandle(hThread);
编译通过。。。perfect!!!!
下面附上<thread.h>的方法
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
void thread()
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep(100);
}
}
int main()
{
thread task(thread);
task.join();
/***
join方法可能会导致子进程被主进程阻塞,可通过detach分离进程的方式改善
task.detach();
***/
return 0;
}