using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InjectionSearch
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>

        public static SqlConnection DBConn;   //数据库连接串
        public static string station;
        public static string StationID;
        public static string OperatorName;
        public static string IP;
        public static string Shift;   //班次
        public static string ProductDate;
        
        [STAThread]
        static void Main()
        {
            bool isRuned;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, "OnlyRunOneInstance", out isRuned);

            //处理UI线程异常

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //处理非UI线程异常

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            if (isRuned)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmLogin());
                mutex.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("程序已启动!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.Exception, e.ToString());
            MessageBox.Show(str);
            //LogManager.WriteLog(str);
        }



        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
            MessageBox.Show(str);
            //LogManager.WriteLog(str);
        }
        static string GetExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("****************************异常文本****************************");

            sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());

            if (ex != null)
            {

                sb.AppendLine("【异常类型】:" + ex.GetType().Name);

                sb.AppendLine("【异常信息】:" + ex.Message);

                sb.AppendLine("【堆栈调用】:" + ex.StackTrace);

            }

            else

            {

                sb.AppendLine("【未处理异常】:" + backStr);

            }

            sb.AppendLine("***************************************************************");

            return sb.ToString();
        }
    }
}