Examples of Winform DevExpress single operating procedures and methods using non DevExpress Mutex method program running and running a single instance of the activated form of realization

Original: DevExpress embodiment of Winform single operating procedures and methods using non DevExpress Mutex method program running and running a single instance of the activated form of realization

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u012097590/article/details/92806194

Online concerning C # singleton method to run the program are relatively simple, some are even unable to realize the function, do not know they have not tried to test the post, because before their own are using third-party controls DevExpress, Singleton also use it to run itself comes with a method, this method is called the need to refer to DevExpress.DevAV.v17.1.Data.dll DevExpress


   
   
  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. }

Lazy before, see a ready-Take-shelf, but also did not think about how to achieve specific, just someone asked, before deciding to go look at the source and found DevExpress This method was originally used Mutex to achieve integration of a moment data, re-writing a C # desktop application for generic singleton method of operation, as follows:

Which you need to reference the following namespace:


   
   
  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. }

Call demo below, pro-test can not be achieved if the discussion, please leave a message ( this article is the original article, reproduced, please, pirated reserved, that article can help you point a praise support, thank you ):


   
   
  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. }

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11611047.html