using System.Windows; namespace SolidWorksListener { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { public static Listener listener; private static System.Threading.Mutex mutex; //系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次 //第二个参数可以设置为产品的名称:Application.ProductName // 每次启动应用程序,都会验证名称为OnlyRun的互斥是否存在 protected override void OnStartup(StartupEventArgs e) { mutex = new System.Threading.Mutex(true, Application.Current.ToString()); if (mutex.WaitOne(0, false)) { base.OnStartup(e); } else { this.Shutdown(); } } private void Application_Startup(object sender, StartupEventArgs e) { listener = new Listener(); listener.Start(); } } }