private static void StoreManagerIntoFile(FrmMain uiform)
        {
            LocalStorage store = new LocalStorage();
            store.LogLevelFlags = uiform.ButtonStatusToLevelFlags();
            store.DomainList = DomainManager.DomainList;
            store.CurrentTaskManager = m_tms;
            store.CurrentTransmissionManager = m_ttm;
            store.ServerTcpPort = ServerPort;
            store.LogReservedDays = LogReservedDays;
            store.LogLoadingLimit = LogLoadingLimit;
            store.IsMinToSystemTray = IsMinToSystemTray;
            store.IsServerModeLocal = !IsServerModeRemote;
            store.MessagePeriod = MessagePeriod;
            store.IsUsingFetion = IsUsingFetion;
            store.FetionNumber = FetionNumber;
            store.FetionPsd = FetionPsd;
            store.FetionSendingTime = FetionSendingTime;
            store.ADSLEntryName = ADSLEntry;
            store.ADSLUserName = ADSLUserName;
            store.ADSLPassword = ADSLPassword;

            IFormatter formatter = new BinaryFormatter();
            try
            {
                Stream stream = new FileStream("SlowAndSteadyParser.store", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, store);
                stream.Close();
                
            }
            catch (Exception e)
            {
                MessageBox.Show("写入本地数据文件失败,当前任务信息无法保存", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                log.Error("写入本地数据文件失败", e);
            }
            finally
            {
                
            }
        }
        public static void Init(FrmMain uiform)
        {
            //Init Thread Name
            Thread.CurrentThread.Name = "UI";

            //Init WebBrowser Pool
            CWBPool.Init(uiform);

            //Init PerformanceBalancer
            PerformanceBalancer.Init(false);
            
            //Init FetionTimer
            uiform.timerMain.Enabled = true;

            //Init TaskManager & TaskTransmissionManager
            ms_FetionSendingTime = new Dictionary<int, bool>();
            for (int i = 0; i < 24; i++)
            {
                ms_FetionSendingTime.Add(i, false);
            }
            LoadManagerFromFile(uiform);
            m_tms.TaskTransmissionManager = m_ttm;
            m_ttm.TaskManagerServer = m_tms;
            
            //open tcp server
            try
            {
                ConnectionManagerServer.RegisterTcpChannel(ms_socketport);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message+Environment.NewLine+"请重新配置端口号", "注册端口错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                log.Error("注册端口错误: ", e);
            }

            //Test Log DB
            string errmessage = null;
            if (LogDatabaseManager.TestConnection(ref errmessage) == false)
                MessageBox.Show(errmessage, "日志数据库", MessageBoxButtons.OK, MessageBoxIcon.Error);

            //Start Tcp Service
            string errormessage = null;
            if (!ConnectionManagerServer.StartTcpServer(m_ttm, ref errormessage))
            {
                MessageBox.Show(errormessage, "启动服务错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }           

        }
        public static void Dispose(FrmMain uiform)
        {
            if (ms_IsServerRunning)
                Stop();

            Thread.Sleep(500);

            //Stop Tcp Service
            ConnectionManagerServer.StopServer();

            ConnectionManagerServer.UnregisterChannel();

            StoreManagerIntoFile(uiform);

            m_tms.Dispose();
            m_ttm.Dispose();

            //Static Manager Disposing...
            VBAStaticEngine.DisposeAll();
            CWBPool.Dispose();
            PerformanceBalancer.Dispose();
        }
        private static void LoadManagerFromFile(FrmMain uiform)
        {
            
            IFormatter formatter = new BinaryFormatter();
            try
            {
                Stream stream = new FileStream("SlowAndSteadyParser.store", FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    LocalStorage store = (LocalStorage)formatter.Deserialize(stream);
                    uiform.LevelFlagsToButtonStatus(store.LogLevelFlags);
                    m_tms = store.CurrentTaskManager;
                    m_ttm = store.CurrentTransmissionManager;
                    DomainManager.DomainList = store.DomainList;
                    IsMinToSystemTray = store.IsMinToSystemTray;
                    IsServerModeRemote = !store.IsServerModeLocal;
                    if (store.ServerTcpPort != 0) ServerPort = store.ServerTcpPort;
                    if (store.LogLoadingLimit != 0) LogLoadingLimit = store.LogLoadingLimit;
                    if (store.LogReservedDays != 0) LogReservedDays = store.LogReservedDays;
                    if (store.MessagePeriod != 0) MessagePeriod = store.MessagePeriod;
                    IsUsingFetion = store.IsUsingFetion;
                    if (store.FetionNumber != null) FetionNumber = store.FetionNumber;
                    if (store.FetionPsd != null) FetionPsd = store.FetionPsd;
                    if (store.FetionSendingTime != null) FetionSendingTime = store.FetionSendingTime;
                    if (store.ADSLEntryName != null) ADSLEntry = store.ADSLEntryName;
                    if (store.ADSLUserName != null) ADSLUserName = store.ADSLUserName;
                    if (store.ADSLPassword != null) ADSLPassword = store.ADSLPassword;
                }
                catch (SerializationException e)
                {
                    m_tms = new TaskManagerServer();
                    m_ttm = new TaskTransmissionManager();
                    log.Warn("本地数据文件格式错误", e);
                }
                catch (Exception e)
                {
                    m_tms = new TaskManagerServer();
                    m_ttm = new TaskTransmissionManager();
                    log.Warn("本地数据文件格式错误", e);
                }
                finally
                {
                    stream.Close();
                }

            }
            catch (System.IO.FileNotFoundException e)
            {
                m_tms = new TaskManagerServer();
                m_ttm = new TaskTransmissionManager();
                log.Warn("未找到本地数据文件", e);
            }
            finally
            {
                if (m_ttm == null) m_ttm = new TaskTransmissionManager();
                if (m_tms == null) m_tms = new TaskManagerServer();
            }
        }
        public static void Dispose(FrmMain uiform)
        {
            if (ms_IsClientRunning)
                Stop();

            Thread.Sleep(500);

            ConnectionManagerClient.UnregisterChannel();         

            StoreManagerIntoFile(uiform);

            ms_tmc.Dispose();

            //Static Manager Disposing...
            VBAStaticEngine.DisposeAll();
            CWBPool.Dispose();
            PerformanceBalancer.Dispose();
        }
        public static void Init(FrmMain uiform)
        {
            Thread.CurrentThread.Name = "UI";
            ms_ui = uiform;
            
            //Init PerformanceBalancer
            PerformanceBalancer.Init(true);

            //Init Web Browser Warehouse
            log.Info("Init Web Browser Pool...");
            CWBPool.Init(ms_ui);

            //Init Channel
            RegisterTcpChannelForClient();

            //Init TaskManager & TaskTransmissionManager
            LoadManagerFromFile(uiform);

        }
        private static void LoadManagerFromFile(FrmMain uiform)
        {
            
            IFormatter formatter = new BinaryFormatter();
            try
            {
                Stream stream = new FileStream("SlowAndSteadyParser.store", FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    LocalStorage store = (LocalStorage)formatter.Deserialize(stream);
                    ms_tmc = store.CurrentTaskManager;
                    DomainManager.DomainList = store.DomainList;
                    ms_c = store.Configuration;                    
                }
                catch (SerializationException e)
                {
                    log.Warn("本地数据文件格式错误", e);
                }
                finally
                {                    
                    stream.Close();
                }

            }
            catch (System.IO.FileNotFoundException e)
            {
                log.Warn("未找到本地数据文件", e);
            }
            finally
            {            
                if (ms_tmc == null) ms_tmc = new TaskManagerClient();
                RecheckConfiguration();
                SetConfigurationToServerInfo();
                SetConfigurationToManager();
                SetConfigurationToPerformanceBalancer();
                SetConfigurationToADSL();
            }
        }
 private static void StoreManagerIntoFile(FrmMain uiform)
 {
     LocalStorage store = new LocalStorage();           
     store.DomainList = DomainManager.DomainList;
     store.CurrentTaskManager = ms_tmc;
     store.Configuration = ms_c;
     IFormatter formatter = new BinaryFormatter();
     try
     {
         Stream stream = new FileStream("SlowAndSteadyParser.store", FileMode.Create, FileAccess.Write, FileShare.None);
         formatter.Serialize(stream, store);
         stream.Close();
         
     }
     catch (Exception e)
     {
         MessageBox.Show("写入本地数据文件失败,客户端信息无法保存", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         log.Error("写入本地数据文件失败", e);
     }
     finally
     {
         
     }
 }