当我们需要只启动一个C#程序实例的时候,一个比较标准的做法是,在程序入口使用一个互斥量来判断是否本程序已经启动,如果程序已经启动则将已经启动的程序激活。代码如下(以WPF为例):
App.xaml:
<Application x:Class="TestOneInstanceOnly.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
namespace TestOneInstanceOn