private void CheckScreenThread(object args) { int n, last_n, cb_n; last_n = (int)args; while (true) { n = ScreenShots.GetScreens().Length; if (n != last_n) { last_n = n; cb_n = (int)this.Invoke((Func <object>)(() => { return(comboBox_SelectScreen.Items.Count); })); if (n > cb_n) { this.Invoke((Action)(() => { for (int i = comboBox_SelectScreen.Items.Count; i < n; i++) { comboBox_SelectScreen.Items.Add(i.ToString()); } })); } else if (n < cb_n) { this.Invoke((Action)(() => { int temp = -1; bool errFlag = false; bool stoppedThread = false; if (int.Parse(Config.Get(Config.Name_ScreenIndex)) >= n) { Config.Set(Config.Name_ScreenIndex, Config.DefalutScreenIndex.ToString(), true); temp = comboBox_SelectScreen.SelectedIndex; comboBox_SelectScreen.SelectedIndex = Config.DefalutScreenIndex; if (clickThread != null && clickThread.IsAlive) { AbortClickThread(); stoppedThread = true; } errFlag = true; } for (int i = comboBox_SelectScreen.Items.Count - 1; i >= n; i--) { comboBox_SelectScreen.Items.Remove(i.ToString()); } if (errFlag) { ShowError("已丢失序号为" + temp.ToString() + "的屏幕。" + (stoppedThread ? "线程已经被终止。" : "")); } })); } } Thread.Sleep(1000); } }
public Form1() { InitializeComponent(); if (Program.ProgramArguments.HideWhenBoot) { //this.Hide(); this.WindowState = this.FormWindowState.Minimized; } //初始化空控件 lastTabIndexControl.Visible = true; lastTabIndexControl.TabIndex = int.MaxValue; this.Controls.Add(lastTabIndexControl); //初始化点击窗口 clicker.Show(); clicker.Visible = false; //初始化设置面板数据 //模板路径 //FileInfo templateFile = new FileInfo(Config.Get(Config.Name_TemplatePath)); //if (!templateFile.Exists) //{ // Config.Set(Config.Name_TemplatePath, templateFile.FullName, true); // templateFile = new FileInfo(Config.Get(Config.Name_TemplatePath)); //} if (!Utility.CheckMatSource(Config.Get(Config.Name_TemplatePath))) { ShowError("当前模板图片不存在或无法打开或不支持。\n" + Config.Get(Config.Name_TemplatePath)); } label_TemplatePath.Text = Config.Get(Config.Name_TemplatePath); //阈值 trackBar_SetThreshold.Value = byte.Parse(Config.Get(Config.Name_Threshold)); textBox_SetThreshold.Text = byte.Parse(Config.Get(Config.Name_Threshold)).ToString(); //计时器 trackBar_SetTimer.Value = SetTimerPanel_TextToValue(Config.Get(Config.Name_Delay)); textBox_SetTimer.Text = int.Parse(Config.Get(Config.Name_Delay)).ToString(); //屏幕序号 int screenNumber = ScreenShots.GetScreens().Length; for (int i = 0; i < screenNumber; i++) { comboBox_SelectScreen.Items.Add(i.ToString()); } int screenIndex = int.Parse(Config.Get(Config.Name_ScreenIndex)); if (screenIndex >= screenNumber) { Config.Set(Config.Name_ScreenIndex, Config.DefalutScreenIndex.ToString(), true); comboBox_SelectScreen.SelectedIndex = Config.DefalutScreenIndex; } else { comboBox_SelectScreen.SelectedIndex = screenIndex; } StartCheckScreenThread(screenNumber); //启动初始化线程 Thread InitThread = new Thread((ThreadStart)(() => { while (!this.IsHandleCreated) { ; } this.BeginInvoke((Action)(() => { InitHotKey(); this.MinimumSize = this.Size; this.MaximumSize = this.Size; if (Program.ProgramArguments.StartThreadDirectly) { StartClickThread(); } })); })); InitThread.IsBackground = true; InitThread.Start(); }