Program.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. using System.Reflection;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. namespace TaskbarGroups
  11. {
  12. static class Program
  13. {
  14. /// <summary>
  15. /// 全局窗体List,便于相互跳转
  16. /// </summary>
  17. public static List<Form> formList = new List<Form>();
  18. /// <summary>
  19. /// The main entry point for the application.
  20. /// </summary>
  21. [STAThread]
  22. static void Main()
  23. {
  24. // 只运行一个实例
  25. Process instance = RunningInstance();
  26. if ( instance ==null){
  27. Application.EnableVisualStyles();
  28. Application.SetCompatibleTextRenderingDefault(false);
  29. Application.Run(new frmMain());
  30. }else
  31. {
  32. // todo 已启动则打开form
  33. //Application.EnableVisualStyles();
  34. }
  35. }
  36. /// <summary>
  37. /// 检测是否已经启动了一个Application实例
  38. /// </summary>
  39. /// <returns>返回进程id,没有启动返回null</returns>
  40. private static Process RunningInstance()
  41. {
  42. Process current = Process.GetCurrentProcess();
  43. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  44. // 遍历正在有相同名字运行的例程
  45. foreach (Process process in processes)
  46. {
  47. // 忽略现有的例程
  48. if (process.Id != current.Id)
  49. {
  50. // 确保例程从EXE文件运行
  51. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  52. {
  53. // 返回另一个例程实例
  54. return process;
  55. }
  56. }
  57. }
  58. // 没有其它的例程,返回null
  59. return null;
  60. }
  61. }
  62. }