1. 共享内存的方法
Unix: QSharedMemory “owns” the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.
方法1:
(据说这种方法在Linux系统下会有一个内存释放的问题,在某种情况下会引起程序的异常或崩溃)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 确保只运行一次 QSystemSemaphore sema("JAMKey",1,QSystemSemaphore::Open); sema.acquire();// 在临界区操作共享内存 SharedMemory QSharedMemory mem("SystemObject");// 全局对象名 if (!mem.create(1))// 如果全局对象以存在则退出 { QMessageBox::information(0, MESSAGEBOXTXT,"An instance has already been running."); sema.release();// 如果是 Unix 系统,会自动释放。 return 0; } sema.release();// 临界区 |
方法2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include "widget.h" #include <QApplication> #include <QSharedMemory> #include <QMessageBox> int main(int argc, char *argv[]) { QApplication a(argc, argv); QSharedMemory *shareMem = new QSharedMemory(QString("SingleInstanceIdentify")); // if the sharedmemory has not been created, it returns false, otherwise true.But if the application exit unexpectedly, // the sharedmemory will not detach. So, we try twice. // 如果尚未创建的共享内存,则返回false,否则真的。但如果应用程序意外退出,共享内存不会分离。所以,我们尝试两次。 volatile short i = 2; while (i--) { if (shareMem->attach(QSharedMemory::ReadOnly)) /* no need to lock, bcs it's read only */ { shareMem->detach(); } } if (shareMem->create(1)) { Widget w; w.show(); a.exec(); if (shareMem->isAttached()) shareMem->detach(); delete shareMem; } else { QMessageBox::information(NULL, "提示", "您已经运行该程序", QMessageBox::Ok); } return 0; } |
2. 使用QLocalServer和QLocalSocket类
下面是自已的写的代码,主要是在运行第二实例的时候,有一个提示的作用:
1. 切换到当前程序,并将当前程序最大化显示到前面。
2.关闭当前程序的进程,打开新程序。
(注意:需要在你的.pro里加上QT += network)
头文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#ifndef PSA_USR_LOGIN_H #define PSA_USR_LOGIN_H #include <QDialog> #include <QTimeLine> #include <QLocalServer> #include "ui_DlgUsrLogin.h" #define PROCESS_SHOW 1 #define PROCESS_RESTART 2 #define PROCESS_STOP 3 const QString PRO_SHOW = "PRO_SHOW"; const QString PRO_RESTART = "PRO_RESTART"; const QString PRO_STOP = "PRO_STOP"; class CUsrLogin : public QDialog { Q_OBJECT public: CUsrLogin(const QString& serverName, QWidget* parent = NULL); virtual ~CUsrLogin(); int GetIndex() const; bool InitServer(); signals: void sig_newOrder(const QString&); private slots: void slot_ok(); void slot_cancel(); void slot_btnGroupClicked(int); void slot_newConnection(); void slot_readyRead(); void slot_timeFinished(); private: int IsServerRun(const QString & servername); private: Ui::DlgUsrLogin ui; int mIndex; QString mServerName; QLocalServer* mpServer; QTimeLine *mpTimeLine; }; #endif |
源文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
#include "psa_usr_login.h" #include <QButtonGroup> #include <QLocalSocket> CUsrLogin::CUsrLogin( const QString& serverName, QWidget* parent /*= NULL*/ ) : QDialog(parent) , mpServer(NULL) , mpTimeLine(NULL) { ui.setupUi(this); mIndex = 1; mServerName = serverName; QButtonGroup* btnGroup = new QButtonGroup; btnGroup->addButton(ui.btn_showCurPro, PROCESS_SHOW); btnGroup->addButton(ui.btn_openNewPro, PROCESS_RESTART); ui.progressBar->setVisible(false); mpTimeLine = new QTimeLine(3000, this); mpTimeLine->setFrameRange(0, 100); connect(mpTimeLine, SIGNAL(frameChanged(int)), ui.progressBar, SLOT(setValue(int))); connect(mpTimeLine, SIGNAL(finished()), this, SLOT(slot_timeFinished())); connect(btnGroup, SIGNAL(buttonPressed(int)), this, SLOT(slot_btnGroupClicked(int))); connect(ui.btn_ok, SIGNAL(clicked()), this, SLOT(slot_ok())); connect(ui.btn_cancel, SIGNAL(clicked()), this, SLOT(slot_cancel())); } CUsrLogin::~CUsrLogin() { if(mpServer) { QLocalServer::removeServer(mServerName); delete mpServer; mpServer = NULL; } if(mpTimeLine) { mpTimeLine->stop(); delete mpTimeLine; mpTimeLine = NULL; } } int CUsrLogin::GetIndex() const { return mIndex; } void CUsrLogin::slot_ok() { QLocalSocket ls; ls.connectToServer(mServerName); if (ls.waitForConnected()) { switch(mIndex) { case PROCESS_SHOW: { char msg[25] = {0}; memcpy(msg, PRO_SHOW.toStdString().c_str(), PRO_SHOW.length()); ls.write(msg); ls.waitForBytesWritten(); QDialog::accept(); break; } case PROCESS_RESTART: { char msg[25] = {0}; memcpy(msg, PRO_RESTART.toStdString().c_str(), PRO_RESTART.length()); ls.write(msg); ls.waitForBytesWritten(); ui.progressBar->setVisible(true); mpTimeLine->start(); break; } case PROCESS_STOP: { char msg[25] = {0}; memcpy(msg, PRO_STOP.toStdString().c_str(), PRO_STOP.length()); ls.write(msg); ls.waitForBytesWritten(); QDialog::accept(); break; } default: QDialog::accept(); break; } } } void CUsrLogin::slot_cancel() { QDialog::reject(); } void CUsrLogin::slot_btnGroupClicked( int idx) { mIndex = idx; } void CUsrLogin::slot_readyRead() { QLocalSocket *local = static_cast<QLocalSocket *>(sender()); if (!local) return; QTextStream in(local); QString readMsg; readMsg = in.readAll(); emit sig_newOrder(readMsg); } // 判断是否有一个同名的服务器在运行 int CUsrLogin::IsServerRun(const QString & servername) { QLocalSocket ls; ls.connectToServer(servername); if (ls.waitForConnected(1000)) { ls.disconnectFromServer(); ls.close(); return 1; } return 0; } bool CUsrLogin::InitServer() { if (!IsServerRun(mServerName)) { mpServer = new QLocalServer; QLocalServer::removeServer(mServerName); mpServer->listen(mServerName); connect(mpServer, SIGNAL(newConnection()), this, SLOT(slot_newConnection())); return true; } return false; } void CUsrLogin::slot_newConnection() { QLocalSocket *newsocket = mpServer->nextPendingConnection(); connect(newsocket, SIGNAL(readyRead()), this, SLOT(slot_readyRead())); } void CUsrLogin::slot_timeFinished() { if(InitServer()) { QDialog::accept(); } } |
主函数:
在主函数中添加CUsrLogin.. 和 信号槽函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
int main(int argc, char * argv[]) { QApplication app(argc, argv); QString name = "******"; // 自定义程序名称 CUsrLogin login(name); if(!login.InitServer()) { int ret = login.exec(); if(QDialog::Accepted == ret) { if( login.GetIndex() == PROCESS_SHOW || login.GetIndex() == PROCESS_STOP ) { return 1; } } else { return 1; } } MainWindow mainWin; mainWin.show(); QObject::connect(&login, SIGNAL(sig_newOrder(const QString&)), &mainWin, SLOT(slotProcAppMessage(const QString&))); return app.exec(); } |
MainWindow中实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
void MainWindow::slotProcAppMessage( const QString& order) { if(order == PRO_SHOW) { #ifdef WIN32 { activateWindow(); showMinimized(); showMaximized(); } #else { activateWindow(); if(windowState () & Qt::WindowMinimized) { setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive); showMaximized(); show(); } else if(windowState() & Qt::WindowMaximized) { setWindowState(windowState() & Qt::WindowMaximized | Qt::WindowActive); } else { setWindowState(windowState() & Qt::WindowMaximized | Qt::WindowActive); } } #endif } else if(order == PRO_RESTART) { close(); } else if(order == PRO_STOP) { close(); } } |
3. QSingleApplication类
实现原理应该和QLocalServer和QLocalSocket相同。
使用Qt中的QSharedMemory,QLocalServer和QLocalSocket实现(不过需要在你的.pro里加上QT += network)
具体说明可以参考:QSingleApplication
发表评论
要发表评论,您必须先登录。