using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using DevExpress.UserSkins; using DevExpress.Skins; using DevExpress.LookAndFeel; using System.Data; using System.Runtime.InteropServices; using System.Diagnostics; using System.Reflection; using System.Text; using DevExpress.XtraSplashScreen; namespace WCS { static class Program { /// /// 该函数设置由不同线程产生的窗口的显示状态。 /// /// 窗口句柄 /// 指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。 /// 如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。 [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。 /// /// 将被激活并被调入前台的窗口句柄。 /// 如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。 [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //f8331421-62a4-4264-b327-ab63c272f7e9 if (!HslCommunication.Authorization.SetAuthorizationCode("f8331421-62a4-4264-b327-ab63c272f7e9")) { // active failed MessageBox.Show("授权失败!当前程序只能使用24小时!"); return; } Process instance = RunningInstance(); if (instance == null) { #region 初始化加载PLC地址 ///SplashScreenManager.ShowDefaultWaitForm("请稍后...","应用程序加载中..."); Dictionary> typeClass = new Dictionary>(); DataTable table = RfConfig.Create().plcds.Tables[0]; foreach (DataRow row in table.Rows) { string[] plcstr = row["plcvalaue"].ToString().Split('%'); List listPlc = new List(); for (int i = 0; i < plcstr.Length; i++) { if (plcstr[i].ToString().Trim().Length > 0) listPlc.Add(plcstr[i].ToString()); } typeClass.Add(row["vlaue"].ToString(), listPlc); } PLCCommon.PlcFactory.Instance().typeClass = typeClass; #endregion //获取异常信息并记录 Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; RfConfig.Create().GetConfigInfo(); BonusSkins.Register(); SkinManager.EnableFormSkins(); UserLookAndFeel.Default.SetSkinStyle("DevExpress Style"); //SplashScreenManager.CloseForm(); Application.Run(new MainForm()); } else { HandleRunningInstance(instance); } } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); WriteSysLog.SysLog("程序发送异常" + str); } private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = GetExceptionMsg(e.Exception, e.ToString()); WriteSysLog.SysLog("程序发生异常" + str); } /// /// 获取正在运行的实例,没有运行的实例返回null; /// public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { return process; } } } return null; } /// /// 显示已运行的程序。 /// public static void HandleRunningInstance(Process instance) { ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示 SetForegroundWindow(instance.MainWindowHandle); //放到前端 } static string GetExceptionMsg(Exception ex, string backStr) { StringBuilder sb = new StringBuilder(); sb.AppendLine("****************************异常文本****************************"); sb.AppendLine("【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); if (ex != null) { sb.AppendLine("【异常类型】:" + ex.GetType().Name); sb.AppendLine("【异常信息】:" + ex.Message); sb.AppendLine("【堆栈调用】:" + ex.StackTrace); } else { sb.AppendLine("【未处理异常】:" + backStr); } sb.AppendLine("***************************************************************"); return sb.ToString(); } } }