首页 > 技术文章 > windows mobile 只能运行一个程序实例

litian 2016-01-08 13:38 原文

 1 static class Program
 2     {
 3         [System.Runtime.InteropServices.DllImport("coredll.Dll", SetLastError = true)]
 4         private static extern int ReleaseMutex(IntPtr hMutex);
 5 
 6         [System.Runtime.InteropServices.DllImport("coredll.Dll", SetLastError = true)]
 7         private static extern IntPtr CreateMutex(string lpMutexAttributes, bool bInitialOwner, string lpName);
 8         /// <summary>
 9         /// 应用程序的主入口点。
10         /// </summary>
11         [MTAThread]
12         static void Main()
13         {
14             try
15             {
16                 IntPtr hMutex = CreateMutex(null, false, "XXXXX");
17                 if (System.Runtime.InteropServices.Marshal.GetLastWin32Error() != 0183)
18                 {
19                     Application.Run(new FrmMain());
20                 }
21                 else
22                 {
23                     MessageBox.Show("程序已经运行.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
24                     ReleaseMutex(hMutex);
25                 }
26             }
27             catch (Exception)
28             {
29 
30                 MessageBox.Show("发生未知错误,程序退出!");
31                 Application.Exit();
32             }
33 
34         }
35     }

 

推荐阅读