/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); Disconnect(); // If it is m_client?.Dispose(), it will be a CA2213 warning. if (m_client != null) { m_client.Dispose(); } if (m_readTask != null) { m_readTask.Dispose(); } if (m_cancelTokenSource != null) { m_cancelTokenSource.Dispose(); } if (m_image != null) { m_image.Dispose(); } } base.Dispose(disposing); }
public bool Connect(VncConfig a_config) { // Connect Vnc m_client?.Dispose(); m_client = new VncClient(a_config); m_client.DisconnectedEvent += (s, e) => { if (InvokeRequired) { Invoke((MethodInvoker) delegate { DisconnectedEvent?.Invoke(s, e); }); } else { DisconnectedEvent?.Invoke(s, e); } }; bool retVal = m_client.ConnectVnc(); if (!retVal) { m_client.Dispose(); m_client = null; return(false); } // Draw the whole for the first time. m_needsRedraw = true; // Initialize zoom ratio for mouseEvent. m_xZoom = new Fraction(this.Width, m_client.ServerInitBody.FramebufferWidth, 10); m_yZoom = new Fraction(this.Height, m_client.ServerInitBody.FramebufferHeight, 10); // Create new image to draw OpenCv Mat. m_image?.Dispose(); m_image = new Bitmap(m_client.ServerInitBody.FramebufferWidth, m_client.ServerInitBody.FramebufferHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // Create read thread m_cancelTokenSource?.Dispose(); m_cancelTokenSource = new CancellationTokenSource(); var token = m_cancelTokenSource.Token; m_readTask?.Dispose(); m_readTask = Task.Factory.StartNew(() => { while (true) { if (m_client != null && m_client.Connected) { // If Read is failed, it returns null. var body = m_client.ReadServerMessage(); if (body == null) { // Disconnect m_client?.DisconnectVnc(); // Execute to draw this control black. // Without this, the screen will not be updated and the VNC image will remain. NativeMethods.InvalidateRect(m_handle, (IntPtr)0, false); return; } if (body.MessageType == VncEnum.MessageTypeServerToClient.FramebufferUpdate) { NativeMethods.InvalidateRect(m_handle, (IntPtr)0, false); lock (m_client.CanvasLock) { m_encodeList.Add(body.EncodeList); } } if (token.IsCancellationRequested) { return; } } } }, m_cancelTokenSource.Token); return(retVal); }