/// <summary> /// 提示用户关闭程序 /// </summary> /// <param name="plist"></param> /// <returns></returns> static void NotifyUserToCloseApp(QueryCloseApplicationEventArgs e) { using (var ca = new CloseApp()) { ca.AttachProcessList(e.Processes); e.IsCancelled = ca.ShowDialog() != DialogResult.OK; } }
/// <summary> /// 关闭主程序进程 /// </summary> bool CloseApplication(RunworkEventArgs e) { string[] argus = Environment.GetCommandLineArgs(); List<Process> closeApplication = new List<Process>(); for (int i = 4; i < argus.Length; i++) { string tid = argus[i]; if (tid.StartsWith("*")) { //TID模式 int t = int.Parse(tid.Trim(new char[] { '*' })); try { Process p = Process.GetProcessById(t); if (p != null) closeApplication.Add(p); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } else { Process[] plist = Process.GetProcessesByName(tid); if (plist.Length > 0) closeApplication.AddRange(plist); } } if (closeApplication.Count > 0) { var evt = new QueryCloseApplicationEventArgs(closeApplication, NotifyUserToCloseApp); e.PostEvent(new SendOrPostCallback(_ => OnQueryCloseApplication(evt))); while (!evt.IsCancelled.HasValue) { System.Threading.Thread.Sleep(100); } return !evt.IsCancelled.Value; } return true; }
/// <summary> /// 引发 <see cref="QueryCloseApplication"/> 事件 /// </summary> protected virtual void OnQueryCloseApplication(QueryCloseApplicationEventArgs e) { if (QueryCloseApplication == null) return; QueryCloseApplication(this, e); }