利用共享内存通信,第二次打开程序时,激活第一次打开的进程
#include <QApplication>
#include <QSharedMemory>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSharedMemory shareMem("Rayvision StartPlayTool");
if (!shareMem.create(1))
{
if (shareMem.attach(QSharedMemory::ReadWrite) && shareMem.lock())
{
*static_cast<uint8_t *>(shareMem.data()) = 1;
shareMem.unlock();
}
return -1;
}
// ... 用户代码
QTimer timer;
timer.callOnTimeout([&] {
shareMem.lock();
if (*static_cast<uint8_t *>(shareMem.data()) != 0)
{
// ... 激活窗口
*static_cast<uint8_t *>(shareMem.data()) = 0;
}
shareMem.unlock();
});
timer.start(10);
return a.exec();
}