qt限制程序只能运行一个

 1 #include <QtWidgets/QApplication>
 2 #include <QSharedMemory>
 3 #include <QMessageBox>
 4 
 5 int AssumeSingleInstance(const QString &key)
 6 {
 7     static QSharedMemory shm(key);
 8     if (shm.create(100) == false)
 9     {
10         return -1;
11     }
12     return 0;
13 }
14 
15 int main(int argc, char *argv[])
16 {
17     QApplication a(argc, argv);
18     //限制程序单实例运行
19     if (AssumeSingleInstance("qwatchDogService.exe") < 0)
20     {
21         QMessageBox::information(NULL, QString::fromLocal8Bit("提示"),
22             QString::fromLocal8Bit("程序已运行"));
23         return -1;
24     }
25 
26     QwatchDogService w;
27     w.show();
28     return a.exec();
29 }

 

posted @ 2024-03-22 09:05  冥天笑  阅读(50)  评论(0编辑  收藏  举报