/// <summary>
		/// 引发 <see cref="QueryCloseApplication"/> 事件
		/// </summary>
		protected virtual void OnQueryCloseApplication(QueryCloseApplicationEventArgs e)
		{
			if (QueryCloseApplication == null)
				return;

			QueryCloseApplication(this, e);
		}
        /// <summary>
        /// 引发 <see cref="QueryCloseApplication"/> 事件
        /// </summary>
        protected virtual void OnQueryCloseApplication(QueryCloseApplicationEventArgs e)
        {
            if (QueryCloseApplication == null)
            {
                return;
            }

            QueryCloseApplication(this, e);
        }
Esempio n. 3
0
        protected override void OnRequestCloseApplication(FSLib.App.SimpleUpdater.QueryCloseApplicationEventArgs e)
        {
            label1.Text = "正在关闭程序..";

            //取消屏蔽下面这行,则会进行默认的处理(询问或自动关闭,依据更新信息的要求来)。如果屏蔽,则必须自己处理进程,并设置 e.IsCancelled 的值。
            //base.OnRequestCloseApplication(e);

            foreach (var p in e.Processes)
            {
                p.Kill();
            }
            //如果需要自定义处理,那么处理完成后一定要赋予一个值,否则会导致更新库对此处的值保持等待。
            //设置为false表示正常结束。返回true则表明出现了问题,要求取消更新
            e.IsCancelled = false;
        }
		/// <summary>
		/// 提示用户关闭程序
		/// </summary>
		/// <returns></returns>
		void NotifyUserToCloseApp(QueryCloseApplicationEventArgs e)
		{
			//强制结束进程
			if (Context.UpdateInfo.ForceKillProcesses)
			{
				foreach (var p in e.Processes)
				{
					try
					{
						p.Kill();
					}
					catch (Exception ex)
					{
						Context.Exception = ex;
						e.IsCancelled = true;
					}
				}
				e.IsCancelled = false;
				return;
			}

			using (var ca = new CloseApp())
			{
				ca.AttachProcessList(e.Processes);
				e.IsCancelled = ca.ShowDialog() != DialogResult.OK;
			}
		}
		/// <summary>
		/// 关闭主程序进程
		/// </summary>
		bool CloseApplication(RunworkEventArgs e)
		{
			Trace.TraceInformation("开始关闭进程");

			e.ReportProgress(0, 0, "正在关闭进程....");
			var closeApplication = new List<Process>();

			foreach (var pid in Context.ExternalProcessID)
			{
				try
				{
					closeApplication.Add(Process.GetProcessById(pid));
					Trace.TraceInformation("添加进程PID=" + pid + "到等待关闭列表");
				}
				catch (Exception ex)
				{
					Trace.TraceInformation("添加进程PID=" + pid + "到等待关闭列表时出错:" + ex.Message);
				}
			}
			foreach (var pn in Context.ExternalProcessName)
			{
				closeApplication.AddRange(Process.GetProcessesByName(pn));
				Trace.TraceInformation("添加进程名=" + pn + "到等待关闭列表");
			}

			if (closeApplication.Count > 0)
			{
				//是否强制关闭进程?
				if (Context.AutoKillProcesses)
				{
					closeApplication.ForEach(s => s.Kill());
					return true;
				}

				var evt = new QueryCloseApplicationEventArgs(closeApplication, NotifyUserToCloseApp);
				e.PostEvent(_ => OnQueryCloseApplication(evt));
				while (!evt.IsCancelled.HasValue)
				{
					Thread.Sleep(100);
				}
				return !evt.IsCancelled.Value;
			}

			return true;
		}