DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法

时间:2023-03-09 20:31:32
DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法

原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

网上关于C#单例运行程序的方法都是比较简单,有些甚至是无法实现功能的,不知道他们试没试过就发帖,因为自己之前都是用第三方控件DevExpress,单例运行也是用它本身自带的一个方法,调用此方法需要引用DevExpress的DevExpress.DevAV.v17.1.Data.dll


  1. static void Main()
  2. {
  3. var appName= Process.GetCurrentProcess().ProcessName;
  4. using (DevExpress.Internal.DevAVDataDirectoryHelper.SingleInstanceApplicationGuard(appName, out exit))
  5. {
  6. if (exit)
  7. return;
  8. Application.EnableVisualStyles();
  9. Application.SetCompatibleTextRenderingDefault(false);
  10. Application.Run(new Form1());
  11. }
  12. }

以前懒,看到有现成的就拿现成的,也没去想具体如何实现,刚好有人问起,才决定去看看源码,发现DevExpress的这个方法原来是用了Mutex来实现的,整合了一下资料,重新写一个供C#桌面应用程序通用的单例运行方法,代码如下:

其中需要引用以下命名空间:


  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Threading;

  1. /// <summary>
  2. /// 单实例应用程序--by涛神
  3. /// </summary>
  4. public class SingleInstanceApplication
  5. {
  6. /// <summary>
  7. /// 判断应用是否运行
  8. /// </summary>
  9. /// <param name="processName"></param>
  10. /// <param name="exit"></param>
  11. /// <returns></returns>
  12. public static IDisposable Guard(out bool exit)
  13. {
  14. Process currentProcess = Process.GetCurrentProcess();
  15. var processName = currentProcess.ProcessName;
  16. Mutex mutex = new Mutex(true, processName, out bool createNew);
  17. if (createNew)
  18. {
  19. exit = false;
  20. }
  21. else
  22. {
  23. Process[] processesByName = Process.GetProcessesByName(currentProcess.ProcessName);
  24. int num = 0;
  25. while (num < (int)processesByName.Length)
  26. {
  27. Process process = processesByName[num];
  28. if (process.Id == currentProcess.Id || !(process.MainWindowHandle != IntPtr.Zero))
  29. {
  30. num++;
  31. }
  32. else
  33. {
  34. WinApiHelper.SetForegroundWindow(process.MainWindowHandle);
  35. WinApiHelper.RestoreWindowAsync(process.MainWindowHandle);
  36. break;
  37. }
  38. }
  39. exit = true;
  40. }
  41. return mutex;
  42. }
  43. private static class WinApiHelper
  44. {
  45. [SecuritySafeCritical]
  46. public static bool IsMaxmimized(IntPtr hwnd)
  47. {
  48. WinApiHelper.Import.WINDOWPLACEMENT wINDOWPLACEMENT = new WinApiHelper.Import.WINDOWPLACEMENT();
  49. if (!WinApiHelper.Import.GetWindowPlacement(hwnd, ref wINDOWPLACEMENT))
  50. {
  51. return false;
  52. }
  53. return wINDOWPLACEMENT.showCmd == WinApiHelper.Import.ShowWindowCommands.ShowMaximized;
  54. }
  55. [SecuritySafeCritical]
  56. public static bool RestoreWindowAsync(IntPtr hwnd)
  57. {
  58. return WinApiHelper.Import.ShowWindowAsync(hwnd, (WinApiHelper.IsMaxmimized(hwnd) ? 3 : 9));
  59. }
  60. [SecuritySafeCritical]
  61. public static bool SetForegroundWindow(IntPtr hwnd)
  62. {
  63. return WinApiHelper.Import.SetForegroundWindow(hwnd);
  64. }
  65. private static class Import
  66. {
  67. [DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
  68. public static extern bool GetWindowPlacement(IntPtr hWnd, ref WinApiHelper.Import.WINDOWPLACEMENT lpwndpl);
  69. [DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
  70. public static extern bool SetForegroundWindow(IntPtr hWnd);
  71. [DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
  72. public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  73. public enum ShowWindowCommands
  74. {
  75. Hide,
  76. Normal,
  77. ShowMinimized,
  78. ShowMaximized,
  79. ShowNoActivate,
  80. Show,
  81. Minimize,
  82. ShowMinNoActive,
  83. ShowNA,
  84. Restore,
  85. ShowDefault,
  86. ForceMinimize
  87. }
  88. public struct WINDOWPLACEMENT
  89. {
  90. public int length;
  91. public int flags;
  92. public WinApiHelper.Import.ShowWindowCommands showCmd;
  93. public Point ptMinPosition;
  94. public Point ptMaxPosition;
  95. public Rectangle rcNormalPosition;
  96. }
  97. }
  98. }
  99. }

调用的demo如下,亲测可以,若无法实现的请留言讨论(本文章是原创文章,转载请标明,盗版必究,觉得文章对你帮助的可以点个赞表示支持一下,谢谢):


  1. static void Main()
  2. {
  3. using(SingleInstanceApplication.Guard(out bool exit))
  4. {
  5. if (exit)
  6. return;
  7. Application.EnableVisualStyles();
  8. Application.SetCompatibleTextRenderingDefault(false);
  9. Application.Run(new Form1());
  10. }
  11. }