void Re() { //重连步骤 //启动一个非UI线程 System.Threading.Tasks.Task.Factory.StartNew(() => { is_start = false; socket.Close(); socket.Dispose(); if (context != System.IntPtr.Zero) { //用非ui线程去关闭DeleteStopContext ControlRemote.DeleteStopContext(context); context = System.IntPtr.Zero; } //ui线程 然后用this.Invoke 去创建 this.ctrl_pic this.Invoke(new Action(() => { //加一句 this.ctrl_pic.Dispose(); this.ctrl_pic = new PictureBox(); this.Controls.Add(this.ctrl_pic); //设置ctrl_panel外观位置 this.ctrl_pic.BackColor = System.Drawing.SystemColors.Desktop; this.ctrl_pic.Location = new System.Drawing.Point(0, 0); this.ctrl_pic.Margin = new System.Windows.Forms.Padding(0); //鼠标事件 this.ctrl_pic.MouseWheel += Ctrl_pic_MouseWheel; this.ctrl_pic.MouseDown += Ctrl_pic_MouseDown; this.ctrl_pic.MouseUp += Ctrl_pic_MouseUp; this.ctrl_pic.MouseMove += Ctrl_pic_MouseMove; this.ctrl_pic.MouseEnter += Ctrl_pic_MouseEnter; this.ctrl_pic.MouseLeave += Ctrl_pic_MouseLeave; })); //最后init 去链接 Init(); }); }
private void RemoteDesktop_FormClosing(object sender, FormClosingEventArgs e) { socket.Close(); socket.Dispose(); Task.Factory.StartNew(() => { lock (lock_object) { if (context != System.IntPtr.Zero) { ControlRemote.DeleteStopContext(context); context = System.IntPtr.Zero; } is_close = true; } }); if (!is_close) { e.Cancel = true; } }