保证应用程序只有一个实例在运行

[C#]
public static Process RunningInstance()
{
     Process current = Process.GetCurrentProcess();
     Process[] processes = Process.GetProcessesByName (current.ProcessName);
      //Loop through the running processes in with the same name
      foreach (Process process in processes)
      {
          //Ignore the current process
           if (process.Id != current.Id)
          {
               //Make sure that the process is running from the exe file.
                if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
                {
                    //Return the other process instance.
                    return process;
                }
           }
      }
      //No other instance was found, return null.
      return null;
}

[VB.NET]
Public Shared Function RunningInstance() As Process
     Dim current As Process = Process.GetCurrentProcess()
     Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

     'Loop through the running processes in with the same name
     Dim process As Process
     For Each process In processes
          'Ignore the current process
          If process.Id <> current.Id Then
               'Make sure that the process is running from the exe file.
               If [Assembly].GetExecutingAssembly().Location.Replace("/", "/") = current.MainModule.FileName Then
                    'Return the other process instance.
                    Return process
               End If
          End If
     Next process
     'No other instance was found, return null.
     Return Nothing
End Function 'RunningInstance

如果检测到已有窗体实例,将此实例显示出来参考:

[STAThread]
static void Main()
{
       //Get the running instance.
       Process instance = RunningInstance();
       if (instance == null)
       {
              //There isn't another instance, show our form.
              Application.Run (new Form2());
       }
       else
       {
              //There is another instance of this process.
              HandleRunningInstance(instance);
       }
}
public
static Process RunningInstance()
{
       Process current = Process.GetCurrentProcess();
       Process[] processes = Process.GetProcessesByName (current.ProcessName);
       //Loop through the running processes in with the same name
       foreach (Process process in processes)
       {
              //Ignore the current process
              if (process.Id != current.Id)
              {
                     //Make sure that the process is running from the exe file.
                     if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") ==current.MainModule.FileName)
                     {
                            //Return the other process instance.
                            return process;
                     }
              }
       }
       //No other instance was found, return null.
       return null;
}
public
static void HandleRunningInstance(Process instance)
{
       //Make sure the window is not minimized or maximized
       ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
       //Set the real intance to foreground window
       SetForegroundWindow (instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private
static extern bool ShowWindowAsync(    IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private
static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中,可以使用QApplication和QMutex类来确保同一台机器上只有一个实例运行。具体实现方法如下: 1. 在main函数中,先创建一个QSharedMemory对象,并给它一个唯一的键值。 2. 调用QSharedMemory的create函数,尝试创建该共享内存。如果返回false,则表示该共享内存已经被创建,说明已经有一个实例运行了,此时可以调用QSharedMemory的attach函数来连接到已经存在的共享内存。 3. 如果create函数返回true,则说明该共享内存还没有被创建,这个时候需要调用QApplication的exec函数来启动应用程序,并在程序退出时调用QSharedMemory的detach函数来释放共享内存。 4. 在程序执行过程中,可以使用QMutex来控制多个线程对共享内存的访问,确保同一时间只有一个线程在访问共享内存。 5. 当程序退出时,需要调用QSharedMemory的detach函数来释放共享内存,确保下次启动程序时可以重新创建共享内存。 下面是一个基本的示例代码: ```c++ #include <QApplication> #include <QSharedMemory> #include <QMessageBox> #include <QMutex> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建唯一的键值 const QString memKey = "MyApplicationKey"; // 创建共享内存 QSharedMemory sharedMem(memKey); // 尝试连接共享内存 if (!sharedMem.create(1)) { // 共享内存已经被创建,说明已经有一个实例运行 QMessageBox::critical(nullptr, "Error", "Another instance is already running!"); return 1; } // 创建互斥锁,确保同一时间只有一个线程在访问共享内存 QMutex mutex; mutex.lock(); // 启动应用程序 int ret = app.exec(); // 释放共享内存 sharedMem.detach(); return ret; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值