Program.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. namespace ProjectManagementSystem
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. Application.EnableVisualStyles();
  17. Application.SetCompatibleTextRenderingDefault(false);
  18. //Application.Run(new MainForm());
  19. Program.InitApplication();
  20. }
  21. public static void InitApplication()
  22. {
  23. System.Windows.Forms.Application.ThreadException += Application_ThreadException;
  24. System.AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  25. try
  26. {
  27. bool flag;
  28. using (new System.Threading.Mutex(true, "Global\\" + System.Windows.Forms.Application.ProductName, out flag))
  29. {
  30. if (flag)
  31. {
  32. Application.Run(new MainForm());
  33. }
  34. else
  35. {
  36. MessageBox.Show("应用程序只能启动一个实例!!!");
  37. Application.Exit();
  38. }
  39. }
  40. }
  41. catch (System.Exception exception)
  42. {
  43. MessageBox.Show("应用程序运行异常!!!" + exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  44. Application.Exit();
  45. }
  46. }
  47. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  48. {
  49. Exception exception = e.ExceptionObject as Exception;
  50. MessageBox.Show("应用程序有未处理的异常!!!" + exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  51. }
  52. private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  53. {
  54. MessageBox.Show("应用程序线程异常!!!" + e.Exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  55. }
  56. }
  57. }