Program.cs 2.8 KB
using log4net.Config;
using log4net.Util.TypeConverters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Common;
using System.Windows.Forms;
using DeviceLibrary;
using DeviceLibrary.manager;
using System.IO;

namespace AGVControl
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            XmlConfigurator.Configure(new FileInfo(AppConfigHelper.GetValue(SettingString.log4net_configname)));
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //程序只能运行一次
            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)
                {
                    //显示已打开的程序
                    Window_API.ShowWindow(process.MainWindowHandle, Window_API.SW_RESTORE);
                    Window_API.SwitchToThisWindow(process.MainWindowHandle, true);
                    return;
                }
            }
            LogUtil.info("=====程序开始=====");
            NodeManager.InitNodesInfos();
            IOManager.Start();
            DeviceLibrary.manager.MissionManager.InitMission();
            ChargePileManager.Init();
            AGVManager.Init();
            AGVManager.control = new DeviceLibrary.Control();

            AGVManager.control.Start();
            AGVManager.server = new AgvServer();
            AGVManager.server.Start();
            DeviceLibrary.manager.MissionManager.Open();
            Application.Run(new FrmMain());

            AGVManager.control.Stop();
            AGVManager.server.Stop();
            IOManager.Stop();
            DeviceLibrary.manager.MissionManager.Close();
            LogUtil.info("=====程序结束=====\r\n");
        }


        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogUtil.error("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
           LogUtil.error("Application_ThreadException", e.Exception);
        }

    }
}