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 }
标签:AssumeSingleInstance,return,qt,int,程序,QString,限制,include From: https://www.cnblogs.com/LYF-LIUDAO/p/18088668