I have two threads. One is a backgroundworker which makes a connection with a TCP client and another, the UI thread which has a timer which makes sure that we know when the backgroundworker takes too long by reporting a timeout.
When the TCP client encounters an exception it takes forever for it to report it. So by making into another thread I can keep the UI active. So, when the timer reports the timeout, I would like to be able to close the tcp client connection so that I can use the backgroundworker again for another use and prevent it from throwing an error that it is still busy.
I hope this is self explanatory but here are a couple snippets of my code:
Timer code:
Backgroundworker:
I have read the help documentation for the TCP client and of the BackgroundWorker. I have tried alternative methods of multithreading and still no result.
I need to cancel the TCP Client forcefully in order for my project to work correctly, but as I mentioned, from different thread.
Thanks,
- Isaac K.
When the TCP client encounters an exception it takes forever for it to report it. So by making into another thread I can keep the UI active. So, when the timer reports the timeout, I would like to be able to close the tcp client connection so that I can use the backgroundworker again for another use and prevent it from throwing an error that it is still busy.
I hope this is self explanatory but here are a couple snippets of my code:
Timer code:
vb Code:
CheckTimeout.Interval = 2500 'If the tcp client reported online before timeout then stop timer. If UserStatetxt.ToLower = "online" Then CheckTimeout.Stop() ElseIf UserStatetxt.ToLower = "connecting..." Then 'If it still says that it is connecting then report a timeout. txtStatus.Text = "Timeout" 'Now stop the backgroundworker from being busy by closing the Tcp client connection bwConnect.CancelAsync() 'cancel async doesn't work 'i need something like Server.close() for my tcp client but this is another thread End If
Backgroundworker:
vb Code:
Private Sub bwConnect_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwConnect.DoWork Dim Server As New System.Net.Sockets.TcpClient() Try Server.Connect(txtIP.Text, txtPort.Text) Catch ex As Exception UserStatetxt = "error" UserStateEx = ex.Message Finally If Server.Connected = True Then UserStatetxt = "online" End If Server.Close() End Try End Sub
I have read the help documentation for the TCP client and of the BackgroundWorker. I have tried alternative methods of multithreading and still no result.
I need to cancel the TCP Client forcefully in order for my project to work correctly, but as I mentioned, from different thread.
Thanks,
- Isaac K.