陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

主程序ui线程异常处理方案和只允许同时运行一个进程的方法

2018年4月8日 748点热度 0人点赞 0条评论
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScreenShowManage
{
    static class Program
    {
        //static Boolean createdNew;
        ////同步基元变量   
        //static System.Threading.Mutex instance = new System.Threading.Mutex(true, "TestSingleStart", out createdNew);
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //if (!createdNew)
            //{
            //    MessageBox.Show("系统已经启动当前程序");
            //    Application.Exit();
            //    return;
            //}

            //设置应用程序处理异常方式:ThreadException处理
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainView());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.Exception, e.ToString());
            LoggingHelper.Fatal("严重错误ThreadException:" + str);
            //ReStartManager();

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
            LoggingHelper.Fatal("严重错误UnhandledException:" + str);
            ReStartManager();
        }

        /// <summary>
        /// 生成自定义异常消息
        /// </summary>
        /// <param name="ex">异常对象</param>
        /// <param name="backStr">备用异常消息:当ex为null时有效</param>
        /// <returns>异常字符串文本</returns>
        static string GetExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("****************************异常文本****************************");
            builder.AppendLine("【出现时间】:" + DateTime.Now.ToString());
            if (ex != null)
            {
                builder.AppendLine("【异常类型】:" + ex.GetType().Name);
                builder.AppendLine("【异常信息】:" + ex.Message);
                builder.AppendLine("【堆栈调用】:" + ex.StackTrace);
            }
            else
            {
                builder.AppendLine("【未处理异常】:" + backStr);
            }
            builder.AppendLine("***************************************************************");
            return builder.ToString();
        }

        /// <summary>
        /// 开启重启程序
        /// </summary>
        static void ReStartManager()
        {
            string processPath = AppDomain.CurrentDomain.BaseDirectory;
            Process winHello = new Process();
            try
            {
                winHello.StartInfo.UseShellExecute = true;
                winHello.StartInfo.FileName = processPath + "ReStartManager.exe";
                winHello.StartInfo.CreateNoWindow = true;
                winHello.Start();
                Application.Exit();
            }
            catch (Exception ex)
            {
                LoggingHelper.Fatal("ReStartManager开启重启程序失败:" + ex.StackTrace);
            }
        }
    }
}

 

标签: C# winform
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
js DevExpress centos C# linux nginx winform python
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
WTM部署CentOS验证码错误 js操作localStorage保存本地json文件的方法 ListHelper帮助类,DataTable和List之间转换 提供一个.net的讯飞在线语音合成SDK DreamSkin自定义美化控件-搜索控件 微信小程序 音频播放功能createInnerAudioContext

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS