You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace MaiMuAOI
  11. {
  12. internal static class Program
  13. {
  14. /// <summary>
  15. /// 应用程序的主入口点。
  16. /// </summary>
  17. [STAThread]
  18. static void Main()
  19. {
  20. //string iteType = "-1";
  21. //if (iteType.Split(',').Count(p => p == "-1") > 0)
  22. // return;
  23. Thread.Sleep(500);
  24. Process instance = RunningInstance();
  25. if (instance == null)
  26. {
  27. Application.ThreadException += Application_ThreadException;
  28. Application.EnableVisualStyles();
  29. Application.SetCompatibleTextRenderingDefault(false);
  30. Application.Run(new MainFrm());
  31. }
  32. else
  33. {
  34. MessageBox.Show("当前程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0);
  35. }
  36. }
  37. //不允许有两个程序同时启动
  38. public static Process RunningInstance()
  39. {
  40. Process current = Process.GetCurrentProcess();
  41. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  42. //遍历正在有相同名字运行的例程
  43. foreach (Process process in processes)
  44. {
  45. //忽略现有的例程
  46. if (process.Id != current.Id)
  47. {
  48. //确保例程从EXE文件运行
  49. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
  50. current.MainModule.FileName)
  51. {
  52. //返回另一个例程实例
  53. return process;
  54. }
  55. }
  56. }
  57. //没有其它的例程,返回Null
  58. return null;
  59. }
  60. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  61. {
  62. Exception ex = e.Exception;
  63. using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\Log.txt", true))
  64. {
  65. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  66. sw.WriteLine("Global捕获到未处理异常:" + ex.GetType().ToString());
  67. sw.WriteLine("异常信息:" + ex.Message);
  68. sw.WriteLine("异常堆栈:" + ex.StackTrace);
  69. sw.WriteLine();
  70. }
  71. MessageBox.Show(string.Format("捕获到未处理异常:{0}\r\n异常信息:{1}\r\n异常堆栈:{2}", ex.GetType(), ex.Message, ex.StackTrace));
  72. }
  73. }
  74. }