Program.cs 5.3 KB
using System;
using Model;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using BLL;

namespace SmartScan
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        /// <param name="args"></param>
        [STAThread]
        static void Main(string[] args)
        {
            Environment.CurrentDirectory = Application.StartupPath;

            _ = new Mutex(true, Application.ProductName, out bool ret);
            if (!ret)
            {
                IntPtr formhwnd = FindWindow(null, "BarCode Rule Setting");
                if (formhwnd == IntPtr.Zero)
                    formhwnd = FindWindow(null, "条码规则设置");
                ShowWindow(formhwnd, SW_RESTORE);
                SwitchToThisWindow(formhwnd, true);
                //MessageBox.Show("该程序已经启动", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //BLL.Config.Backgrounder = true;
            bool back = BLL.Config.Backgrounder;
            bool hide = false;
            if (args.Length > 0)
                hide = args[0].ToLower() == "hide";
           // if (Config.UsePaddleOCR)
            {
                var paddle = "paddleOCR.exe";
                Process process = new Process();
                process.StartInfo = new ProcessStartInfo();
                process.StartInfo.FileName = paddle;
                process.StartInfo.WorkingDirectory = ".\\paddle";
                if (File.Exists(".\\paddle\\paddleOCR.exe"))
                    process.Start();

            }
            //else
            //{
            //    var onnxexe = "onnx\\OcrLiteOnnxForm.exe";
            //    Process process1 = Process.Start(onnxexe);
            //}




            Application.Run(new FrmLoading(back));  //预加载,完成后自动退出


            Common.frmMain = new FrmMain();
            Common.frmWaitting = new FrmWaitting();

            if (back)
            {
                var fsp = new FrmSetPlus();
                if (hide)
                    fsp.WindowState = FormWindowState.Minimized;

                Application.Run(fsp);
            }
            else
                Application.Run(Common.frmMain);

            Exit();
        }

        private static bool IsRun()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id == current.Id) continue;  //自己
                if (process.MainModule.FileName == current.MainModule.FileName)
                    return true;
            }
            return false;
        }

        private static void Exit()
        {
            LogNet.log.Info("=====准备退出...=====");
            Common.lightSource.Close();
            Common.ioModule?.Close();
            Common.cameraVision?.Dispose();
            WebService.Close();
            LogNet.log.Info("=====程序结束=====\r\n");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogNet.log.Error("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogNet.log.Error("Application_ThreadException", e.Exception);
        }

        #region Win32函数的声明

        /// <summary>
        /// 找到某个窗口与给出的类别名和窗口名相同窗口 
        /// </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>
        ///  设置窗口的显示状态 
        /// </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);
        public const int SW_RESTORE = 9;

        #endregion
    }
}