/// <summary> /// 多线程异常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { //可以记录日志并转向错误bug窗口友好提示用户 Exception ex = e.ExceptionObject as Exception; string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); DataFunction.txtWrite(str); }
static void Main() { try { //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += Application_ThreadException; ////UI线程异常 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; ////多线程异常 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } catch (Exception ex) { string str = GetExceptionMsg(ex, string.Empty); DataFunction.txtWrite(str); } GC.Collect(); }
/// <summary> /// 获取鼠标坐标和颜色 /// </summary> public void Point_Color() { while (true) { Thread.Sleep(200); try { if (IsGetPoint) { this.Invoke(new Action(() => { System.Drawing.Point p = new System.Drawing.Point(MousePosition.X, MousePosition.Y); //取置顶点坐标 IntPtr hdc = GetDC(new IntPtr(0)); //取到设备场景(0就是全屏的设备场景) string txtStartX = MousePosition.X.ToString(); string txtStartY = MousePosition.Y.ToString(); //label7.Text = "坐标:X:" + txtStartX + ",Y:" + txtStartY; int c = GetPixel(hdc, p); //取指定点颜色 int r = (c & 0xFF); //转换R int g = (c & 0xFF00) / 256; //转换G int b = (c & 0xFF0000) / 65536; //转换B //picturePintColor.BackColor = Color.FromArgb(r, g, b); //label8.Text = "RGB:" + r.ToString() + "," + g.ToString() + "," + b.ToString(); })); } } catch (Exception ex) { //string txtPath = Path.GetFullPath("Error.txt"); DataFunction.txtWrite(ex.ToString()); } finally { GC.Collect(); } } }
/// <summary> /// 获取MES状态 /// </summary> public void MesState() { //获得当前屏幕的分辨率 Screen scr = Screen.PrimaryScreen; Rectangle rc = scr.Bounds; int iWidth = rc.Width; int iHeight = rc.Height; //创建一个和屏幕一样大的Bitmap Image myImage = new Bitmap(iWidth, iHeight - 50); //Image myImage = new Bitmap(688, 520); //从一个继承自Image类的对象中创建Graphics对象 Graphics g = Graphics.FromImage(myImage); string jietustr = string.Empty; while (true) { try { Thread.Sleep(100); if (serial.IsOpen) { this.Invoke(new Action(() => { logNum = Convert.ToInt32(tb_LogNum.Text); })); string imgpath = Path.GetFullPath("images"); imgpath += "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; //抓屏并拷贝到myimage里 g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight)); //保存为文件 myImage.Save(imgpath); //string Contrast_picturePath = imgpath; //byte[] JieTuByte = DataFunction.GetImageByte(Contrast_picturePath); jietustr = DataFunction.GetBase64StringByImage(myImage); //myImage.Dispose(); if (jietustr == SaveImgstr) { string Write = "相同图片"; } else { SaveImgstr = jietustr; string Writestr = string.Empty; string Runstate = string.Empty; this.Invoke(new Action(() => { if (IsMesState(imgpath)) { logNum++; this.Invoke(new Action(() => { Writestr = tb_OK.Text; tb_LogNum.Text = logNum.ToString(); })); } else { this.Invoke(new Action(() => { Writestr = tb_NG.Text; })); } })); Runstate = Writestr; byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(Writestr); Writestr = HexStringTenByte.ByteArrayToHexString(byteArray); byte[] buff = HexStringTenByte.HexString2Bytes(Writestr); serial.Write(buff, 0, buff.Length); this.Invoke(new Action(() => { this.lb_send.Text = "Send:" + DateTime.Now.ToString("HH:mm:ss"); this.tb_send.Text = Runstate; })); } string ImgPath = Path.GetFullPath("images"); int FileNum = DataFunction.GetFileNum(ImgPath); if (FileNum >= 10) { DataFunction.DelectDir(ImgPath); } GC.Collect(); //myImage.Dispose(); } } catch (Exception ex) { DataFunction.txtWrite(ex.ToString()); } GC.Collect(); } }
/// <summary> /// 未捕获线程异常时发生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = GetExceptionMsg(e.Exception, e.ToString()); DataFunction.txtWrite(str); }