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

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);
            }
        }
    }
}
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WinForm程序中,UI界面是由主线程(也称UI线程)负责更新的,其他线程不能直接修改UI界面数据,否则会抛出异常。如果需要跨线程更新UI界面数据,可以使用以下几种方法: 1. 使用Control.Invoke方法:该方法可以将UI线程委托一个方法来执行,从而实现跨线程更新UI界面数据。例如: ``` private void UpdateUI(string data) { if (this.InvokeRequired) { this.Invoke(new Action<string>(UpdateUI), data); } else { label1.Text = data; } } ``` 2. 使用Control.BeginInvoke方法:该方法与Invoke类似,但是它是异步的。它会立即返回,而不是等待委托方法执行完毕。例如: ``` private void UpdateUI(string data) { if (this.InvokeRequired) { this.BeginInvoke(new Action<string>(UpdateUI), data); } else { label1.Text = data; } } ``` 3. 使用SynchronizationContext类:该类可以在不同线程之间传递上下文信息,从而实现跨线程更新UI界面数据。例如: ``` private SynchronizationContext _syncContext; public Form1() { InitializeComponent(); _syncContext = SynchronizationContext.Current; } private void UpdateUI(string data) { _syncContext.Post(new SendOrPostCallback((obj) => { label1.Text = obj.ToString(); }), data); } ``` 以上三种方法都可以实现跨线程更新UI界面数据,根据实际情况和个人习惯选择即可。需要注意的是,跨线程更新UI界面数据是一个比较耗时的操作,如果频繁调用会对程序的性能产生影响。因此,建议仅在必要时使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值