Program.cs 6.8 KB
using log4net.Config; 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common; 

namespace TSA_V
{
    static class Program
    {
        #region 只启动一个实例

        /// <summary>
        /// 找到某个窗口与给出的类别名和窗口名相同窗口
        /// 非托管定义为:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
        /// </summary>
        /// <param name="lpClassName">类别名</param>
        /// <param name="lpWindowName">窗口名</param>
        /// <returns>成功找到返回窗口句柄,否则返回null</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 切换到窗口并把窗口设入前台,类似 SetForegroundWindow方法的功能
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="fAltTab">True代表窗口正在通过Alt/Ctrl +Tab被切换</param>
        [DllImport("user32.dll ", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        ///// <summary>
        /////  设置窗口的显示状态
        /////  Win32 函数定义为:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
        ///// </summary>
        ///// <param name="hWnd">窗口句柄</param>
        ///// <param name="cmdShow">指示窗口如何被显示</param>
        ///// <returns>如果窗体之前是可见,返回值为非零;如果窗体之前被隐藏,返回值为零</returns>
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //DatumPoint datumPoint = new DatumPoint("YR1", "1", 2, 61.309f, 133.04f, "CRD", 6.06f, -4.31f);
            //DatumPoint datumPoint = new DatumPoint("YR1", "1", 2, 61.309f, 133.04f, "CRD", 0, 0);
            //LearningControl.TrainModel();
            //datumPoint = LearningControl.TestData(datumPoint);
            string ext2v = "111101111111".Substring(4, 1);

            try
            { 
                // 方法四相对于方法三而言应该可以说是一个改进,
                // 因为方法三只能是最小化的窗体显示出来,如果隐藏到托盘中则不能把运行的程序显示出来
                // 具体问题可以看这个帖子:http://social.msdn.microsoft.com/Forums/zh-CN/6398fb10-ecc2-4c03-ab25-d03544f5fcc9
                Process currentproc = Process.GetCurrentProcess();
                Process[] processcollection = Process.GetProcessesByName(currentproc.ProcessName.Replace(".vshost", string.Empty));
                //  该程序已经运行,

                bool isShow = false;
                if (processcollection.Length >= 1)
                {
                    //MessageBox.Show("processcollection.Length >= 1");
                    foreach (Process process in processcollection)
                    {
                        if (process.Id != currentproc.Id)
                        {
                            // 如果进程的句柄为0,即代表没有找到该窗体,即该窗体隐藏的情况时
                            if (process.MainWindowHandle.ToInt32().Equals(0))
                            {

                                //MessageBox.Show(currentproc.Id + "");
                                // 获得窗体句柄
                                string title = ConfigAppSettings.GetValue("app.title");
                                IntPtr formhwnd = FindWindow(null, title);
                                // 重新显示该窗体并切换到带入到前台
                                int SW_RESTORE = 9;
                                ShowWindow(formhwnd, SW_RESTORE);
                                SwitchToThisWindow(formhwnd, true);
                                isShow = true;
                            }
                            else
                            {
                                // 如果窗体没有隐藏,就直接切换到该窗体并带入到前台
                                // 因为窗体除了隐藏到托盘,还可以最小化
                                SwitchToThisWindow(process.MainWindowHandle, true);
                                isShow = true;
                            }
                        }
                    }
                }
                if (!isShow)
                {
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                    ImageManager.Init();
                    XmlConfigurator.Configure();
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);


                    DeviceLibrary.DB.db = new DAL.SQLite();
                    bool rtn = DeviceLibrary.DB.db.Connect();
                    LogUtil.info("登录数据库" + rtn);
                    FrmLogin frm = new FrmLogin();
                    DialogResult dr = frm.ShowDialog();
                    if (dr == DialogResult.OK)
                        Application.Run(new FrmMenu());
                    DeviceLibrary.DB.db.Close();
                    //System.Threading.Thread.CurrentThread.Abort();
                    Process.GetCurrentProcess().Kill();
                    Application.ExitThread();
                }

            }
            catch (Exception ex)
            {
                LogUtil.error(ex.ToString());
            }
      
        }
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogUnhandledException("CurrentDomain_UnhandledException", e.ToString() + "" + e.ExceptionObject.ToString() + " ");
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogUnhandledException("Application_ThreadException", e.ToString() + "" + e.Exception.ToString() + " ");
        }

        static void LogUnhandledException(string type, string exceptionobj)
        {
            //这里可以进一步地写日志
            LogUtil.error("【" + type + "】" + exceptionobj);
            MessageBox.Show(exceptionobj, type);

            LogUtil.error("【" + type + "】" + exceptionobj);
        }
    }
}