版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

79 řádky
2.8 KiB

  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. Thread.Sleep(500);
  21. Process instance = RunningInstance();
  22. if (instance == null)
  23. {
  24. Application.ThreadException += Application_ThreadException;
  25. Application.EnableVisualStyles();
  26. Application.SetCompatibleTextRenderingDefault(false);
  27. Application.Run(new MainFrm());
  28. }
  29. else
  30. {
  31. MessageBox.Show("当前程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0);
  32. }
  33. }
  34. //不允许有两个程序同时启动
  35. public static Process RunningInstance()
  36. {
  37. Process current = Process.GetCurrentProcess();
  38. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  39. //遍历正在有相同名字运行的例程
  40. foreach (Process process in processes)
  41. {
  42. //忽略现有的例程
  43. if (process.Id != current.Id)
  44. {
  45. //确保例程从EXE文件运行
  46. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
  47. current.MainModule.FileName)
  48. {
  49. //返回另一个例程实例
  50. return process;
  51. }
  52. }
  53. }
  54. //没有其它的例程,返回Null
  55. return null;
  56. }
  57. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  58. {
  59. Exception ex = e.Exception;
  60. using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\Log.txt", true))
  61. {
  62. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  63. sw.WriteLine("Global捕获到未处理异常:" + ex.GetType().ToString());
  64. sw.WriteLine("异常信息:" + ex.Message);
  65. sw.WriteLine("异常堆栈:" + ex.StackTrace);
  66. sw.WriteLine();
  67. }
  68. MessageBox.Show(string.Format("捕获到未处理异常:{0}\r\n异常信息:{1}\r\n异常堆栈:{2}", ex.GetType(), ex.Message, ex.StackTrace));
  69. }
  70. }
  71. }