WinFrom 只能运行一个实例以及全局异常处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using UtilityHelper;


namespace LHCity_LMS_Client.Forms
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool _bInitiallyOwned = true;
            bool _bIsCreated;
            Mutex m = new Mutex(_bInitiallyOwned, "ce08415e-55ec-4411-96d2-ea91f404e05a", out _bIsCreated);
            if (!(_bInitiallyOwned && _bIsCreated))
            {
                MessageBox.Show("已有一个程序的实例在运行。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Application.Exit();
            }
            else
            {
                FontOperate.installFont("LCDMono.TTF", "LCDMono");
                FontOperate.installFont("msyh.ttf", "微软雅黑");
                DevExpress.Skins.SkinManager.EnableFormSkins();
                Application.EnableVisualStyles();
                DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
                Application.SetCompatibleTextRenderingDefault(false);


                #region 最高级别的异常处理情况
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += new UnhandledExceptionEventHandler(TopLevelExceptionHandler);
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                #endregion
                Application.Run(new frmLogin());
                //Application.Run(new frmSysParamterCfg());
                DevExpress.Utils.AppearanceObject.DefaultFont = new Font(LHCity_LMS_Client.Configuration.FontSet.sMuduleFontName, LHCity_LMS_Client.Configuration.FontSet.nFontSize);
            }

        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show("非常抱歉,系统出现异常,请重新启动系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
        /// <summary>
        /// 最高级别异常处理情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void TopLevelExceptionHandler(object sender, UnhandledExceptionEventArgs args)
        {
            MessageBox.Show("非常抱歉,系统出现异常,请重新启动系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }



    }
}
posted @ 2013-03-08 11:14  Net-Spider  阅读(254)  评论(0)    收藏  举报