实现单实例应用程序的三种方法

本文介绍三种“实现单实例应用程序”的方法,保障每次只能运行一个该类型的应用程序。

一、共享内存

        在main函数中添加如下代码:


// 使用共享内存的方式来保证只运行一个实例
QString strKey = "MyApplication";
QSharedMemory sharedMemory;
sharedMemory.setKey(strKey);
if (sharedMemory.attach())
{
QMessageBox::information(0,
QObject::tr("Information", "need translate"),
QObject::tr("This program is running already.", "need translate"),
QMessageBox::Ok);

return 0;
}


二、创建命名服务器

参考博客:如何让Qt应用程序只运行一个实例

三、创建“跨进程边界的共享内核对象”

参考《Windows核心编程》第三章
---------------------
作者:Sagittarius_Warrior
来源:CSDN
原文:https://blog.csdn.net/Sagittarius_Warrior/article/details/51701340
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/lidabo/p/10248677.html