I have a question how can I reset a form when it will be hide? like its thread started and etc. I just want my form back to normal just like a fresh new launched form when .show again after .hide. Is there a command for that? I am having problem terminating my project. I am currently playing with a Desktop Viewer code I found over the internet . I want to figure out how to terminate if the client will disconnect or the server will disconnect then once the disconnected form will return, it will continue the stream or can resume the stream. I am getting cannot connect error when doing it after closing and reopening my client.
Here is my code
SERVER
CLIENT
Here is my code
SERVER
Server Code:
Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.Drawing Imports System.Runtime.Serialization.Formatters.Binary Public Class Server Dim client As New TcpClient Dim server As New TcpListener(8085) Dim nstream As NetworkStream Dim listening As New Thread(AddressOf Listen) Dim getImage As New Thread(AddressOf receiveImage) Private Sub receiveImage() Try Dim bf As New BinaryFormatter While client.Connected = True nstream = client.GetStream PictureBox1.Image = bf.Deserialize(nstream) End While Catch ex As Exception CheckForIllegalCrossThreadCalls = False Button1.Text = "Listen" End Try End Sub Private Sub Listen() While client.Connected = False server.Start() client = server.AcceptTcpClient End While getImage.Start() End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub Private Sub Server_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Button1.Text = "Listen" Then Try listening.Start() Catch ex As Exception If ex.Message.Contains("Thread is running or terminated; it cannot restart.") Then server.Start() End If End Try Button1.Text = "Stop Listening" Else client.Close() Button1.Text = "Listen" End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Clientform.Show() End Sub End Class
CLIENT
CLIENT Code:
Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.Drawing Imports System.Runtime.Serialization.Formatters.Binary Public Class Clientform Dim client As New TcpClient Dim nstream As NetworkStream Private Sub Clientform_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing Timer1.Enabled = False Button2.Text = "Stream" End Sub Private Sub Clientform_Leave(sender As Object, e As EventArgs) Handles Me.Leave End Sub Private Sub Client_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Public Function Desktop() As Image Dim bounds As Rectangle = Nothing Dim screenshot As System.Drawing.Bitmap = Nothing Dim graph As Graphics = Nothing bounds = Screen.PrimaryScreen.Bounds screenshot = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) graph = Graphics.FromImage(screenshot) graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy) Return screenshot End Function Private Sub SendImage() Try Dim bf As New BinaryFormatter nstream = client.GetStream bf.Serialize(nstream, Desktop()) Catch ex As Exception Timer1.Enabled = False client.Close() End Try End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick SendImage() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try If Button1.Text = "Connect" Then client.Connect("127.0.0.1", 8085) Button1.Text = ("Disconnect") Else client.Close() Button1.Text = ("Connect") End If Catch ex As Exception MsgBox("Failed to connect...") End Try End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If Button2.Text = "Stream" Then Timer1.Enabled = True Button2.Text = "Stop Stream" Else Timer1.Enabled = False Button2.Text = "Stream" End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Server.Show() End Sub Private Sub Button2_ControlRemoved(sender As Object, e As ControlEventArgs) Handles Button2.ControlRemoved End Sub End Class