Exemplo n.º 1
0
        /// <summary>
        /// 通用的已经运行一个实例的执行动作
        /// </summary>
        public static void GeneralAlreadyRunAction(Action <Exception> exceptionHandler = null)
        {
            try
            {
                #region 强制退出

                int maxWaitMilliSecondBeforeExit = 10000;
                //Task创建的是后台线程
                Task.Factory.StartNew(() =>
                {
                    //5s后强制退出
                    if (maxWaitMilliSecondBeforeExit > 0)
                    {
                        try
                        {
                            Thread.Sleep(maxWaitMilliSecondBeforeExit);
                        }
                        catch
                        {
                            //ignore
                        }
                    }

                    for (int i = 0; i < 8; i++)
                    {
                        try
                        {
                            NLogHelper.Warn($"程序自动第{i + 1}次尝试强制退出");
                            Environment.Exit(0);
                        }
                        catch
                        {
                            //ignore
                            try
                            {
                                Thread.Sleep(10);
                            }
                            catch { }
                        }
                    }
                });

                #endregion

                MessageBox.Show($"已经启动一个实例,请点击确定退出本实例({maxWaitMilliSecondBeforeExit / 1000}秒后自动退出)", "已经启动一个实例",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);

                for (int i = 0; i < 8; i++)
                {
                    try
                    {
                        NLogHelper.Warn($"程序强制第{i + 1}次尝试强制退出");
                        Environment.Exit(0);
                    }
                    catch
                    {
                        //ignore
                        try
                        {
                            Thread.Sleep(10);
                        }
                        catch { }
                    }
                }
            }
            catch (Exception ex)
            {
                if (exceptionHandler != null)
                {
                    exceptionHandler(ex);
                }
                else
                {
                    NLogHelper.Error("程序退出时异常:" + ex);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通用的已经运行一个实例的执行动作
        /// </summary>
        public static void GeneralExitAction(string title, string content, Action <Exception> exceptionHandler = null, int maxWaitMilliSecondBeforeExit = 30000)
        {
            try
            {
                #region 强制退出

                //Task创建的是后台线程
                Task.Factory.StartNew(() =>
                {
                    //5s后强制退出
                    if (maxWaitMilliSecondBeforeExit > 0)
                    {
                        const int sleepPeriod = 100;//每次休眠100毫秒
                        for (int i = 0; i *sleepPeriod < maxWaitMilliSecondBeforeExit; i++)
                        {
                            try
                            {
                                Thread.Sleep(sleepPeriod);
                            }
                            catch
                            {
                                //ignore
                            }
                        }
                    }

                    for (int i = 0; i < 11; i++)
                    {
                        try
                        {
                            NLogHelper.Warn($"程序自动第{i + 1}次尝试强制退出");
                            Environment.Exit(0);
                        }
                        catch
                        {
                            //ignore
                            try
                            {
                                Thread.Sleep(10);
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                    }
                });

                #endregion

                System.Windows.MessageBox.Show(content, title, MessageBoxButton.OK, MessageBoxImage.Warning);

                for (int i = 0; i < 8; i++)
                {
                    try
                    {
                        NLogHelper.Warn($"程序强制第{i + 1}次尝试强制退出");
                        Environment.Exit(0);
                    }
                    catch
                    {
                        //ignore
                        try
                        {
                            Thread.Sleep(10);
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (exceptionHandler != null)
                {
                    exceptionHandler(ex);
                }
                else
                {
                    NLogHelper.Error("程序退出时异常:" + ex);
                }
            }
        }