且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止多次启动我的应用程序?

更新时间:2023-10-16 10:18:40

在程序启动时检查同一进程是否已经运行:

 使用System.Diagnostics; 

static void Main(string [] args)
{
String thisprocessname = Process.GetCurrentProcess()。

if(Process.GetProcesses()。Count(p => p.ProcessName == thisprocessname)> 1)
return;
}


I deployed my C# WinForms application using ClickOnce installation. Everything works fine with it (after a lot of work) :), but now I'm facing a problem:

Whenever I click on the application shortcut in the Start menu, a new instance starts. I need to avoid this.

What can I do to prevent multiple launches?

At program startup check if same process is already running:

using System.Diagnostics;

static void Main(string[] args)
{
   String thisprocessname = Process.GetCurrentProcess().ProcessName;

   if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
      return;           
}