
using Models;
using ProductionControl.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ProductionControl
{
    internal static class Program
    { 
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.Run(new Form3());
            //return;
            //byte[] buff = File.ReadAllBytes(@"D:\AZTCode\HBBank\证书\国mi-生产证书ssl\CFCA_ACS_SM2_OCA31.cer");
            //string s = Convert.ToBase64String(buff);

            //string bmppath = @"f:\abc.bmp";
            //Order order=new Order();
            //order.DefectInfoList = new List<DefectInfo>() { 
            //    new DefectInfo(){ X=100,Y=100, Code="dk"},
            //    new DefectInfo(){ X=200,Y=100, Code="dk"},
            //    new DefectInfo(){ X=100,Y=200, Code="zk"},
            //    new DefectInfo(){ X=200,Y=220, Code="xws"},
            //};
            //Application.Run(new FrmShowDefectImage(bmppath, order));
            //return;

            Thread.Sleep(500);
            Process instance = RunningInstance();
            if (instance == null)
            {
                Application.ThreadException += Application_ThreadException;

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Config.LoadAllConfig(); 
                Service.InitDB.ConnectionString = Config.DBConStr;
                //Application.Run(new FrmScannerShow2(new Size(300,300)));
                Application.Run(new FrmLogin());
            }
            else
            {
                MessageBox.Show("当前程序已经运行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0);
            }
        }
        
        //不允许有两个程序同时启动
        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            //遍历正在有相同名字运行的例程 
            foreach (Process process in processes)
            {
                //忽略现有的例程 
                if (process.Id != current.Id)
                {
                    //确保例程从EXE文件运行 
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
                    current.MainModule.FileName)
                    {
                        //返回另一个例程实例 
                        return process;
                    }
                }
            }
            //没有其它的例程，返回Null 
            return null;
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {

            Exception ex = e.Exception;
            using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\Log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.WriteLine("Global捕获到未处理异常：" + ex.GetType().ToString());
                sw.WriteLine("异常信息：" + ex.Message);
                sw.WriteLine("异常堆栈：" + ex.StackTrace);
                sw.WriteLine();
            }
            MessageBox.Show(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}\r\n异常堆栈：{2}", ex.GetType(), ex.Message, ex.StackTrace));

        }
    }
}
