禁止程序多开,在c#中这样处理:
winform启动添加:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process[] processes = Process.GetProcesses();
Process currentProcess = Process.GetCurrentProcess();
bool processExist = false;
foreach (Process p in processes)
{
if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
{
processExist = true;
}
}
if (processExist)
{
Application.Exit();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
wpf可以卸载App.xaml的后台:
public partial class App : Application
{
private static System.Threading.Mutex mutex;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
// 每次启动应用程序,都会验证名称为OnlyRun的互斥是否存在
protected override void OnStartup(StartupEventArgs e)
{
mutex = new System.Threading.Mutex(true, "OnlyRun"); if (mutex.WaitOne(0, false))
{
base.OnStartup(e);
}
else { MessageBox.Show("程序已经在运行!", "提示",MessageBoxButton.OK,MessageBoxImage.Information); this.Shutdown(); }
}
}