学海无涯

导航

实现单应用程序,即只能启动一个实例

 1 using Prism.Ioc;
 2 using System.Threading;
 3 using System.Windows;
 4 using WpfTestBlankApp.Views;
 5 
 6 namespace WpfTestBlankApp
 7 {
 8     /// <summary>
 9     /// Interaction logic for App.xaml
10     /// </summary>
11     public partial class App
12     {
13         private Mutex m_Mutex;
14         protected override Window CreateShell()
15         {
16             return Container.Resolve<MainWindow>();
17         }
18 
19         protected override void RegisterTypes(IContainerRegistry containerRegistry)
20         {
21             containerRegistry.RegisterForNavigation<HolleView>();
22         }
23         protected override void OnStartup(StartupEventArgs e)
24         {//实现单应用程序,即只能启动一个实例
25             base.OnStartup(e);
26             string mutexName = "MyCompanyName.MyAppName";
27             bool createdNew;
28             m_Mutex = new Mutex(true, mutexName, out createdNew);
29             if (!createdNew)
30             {
31                 Shutdown();
32             }
33         }
34 
35 
36     }
37 }

 

posted on 2023-01-05 17:09  宁静致远.  阅读(31)  评论(0编辑  收藏  举报