Program.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ComponentFactory.Krypton.Toolkit;
  2. using FastReport;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. namespace NXWMS
  10. {
  11. static class Program
  12. {
  13. /// <summary>
  14. /// 应用程序的主入口点。
  15. /// </summary>
  16. public static ApplicationContext context;
  17. /// <summary>
  18. /// 应用程序的主入口点。
  19. /// </summary>
  20. [STAThread]
  21. static void Main()
  22. {
  23. try
  24. {
  25. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  26. Application.ThreadException += Application_ThreadException;
  27. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  28. new EnvironmentSettings().ReportSettings.ShowProgress = false;
  29. var processName = Process.GetCurrentProcess().ProcessName;
  30. if ((Process.GetProcessesByName(processName)).GetUpperBound(0) > 0)
  31. {
  32. KryptonMessageBox.Show(@"系统已经在运行中,如果要重新启动,请从进程中关闭...", @"系统警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  33. }
  34. else
  35. {
  36. Application.EnableVisualStyles();
  37. Application.SetCompatibleTextRenderingDefault(false);
  38. var upgrade = new FrmLoginNew();
  39. context = new ApplicationContext();
  40. context.Tag = upgrade;
  41. Application.Idle += Application_Idle; //注册程序运行空闲去执行主程序窗体相应初始化代码
  42. Application.Run(context);
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. KryptonMessageBox.Show("系统出现未知异常,请重启系统:" + ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  48. }
  49. }
  50. private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  51. {
  52. var ex = e.Exception.InnerException;
  53. if (ex != null)
  54. {
  55. KryptonMessageBox.Show("系统出现未知异常," + ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  56. }
  57. }
  58. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  59. {
  60. var ex = e.ExceptionObject as Exception;
  61. if (ex != null)
  62. {
  63. KryptonMessageBox.Show("系统出现未知异常," + ex.InnerException.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  64. }
  65. }
  66. private static void Application_Idle(object sender, EventArgs e)
  67. {
  68. Application.Idle -= Application_Idle;
  69. if (context.MainForm == null)
  70. {
  71. var login = new FrmLoginNew();
  72. context.MainForm = login;
  73. var sp = (FrmLoginNew)context.Tag;
  74. sp.Close();
  75. login.Show();
  76. }
  77. }
  78. }
  79. }