Threading and the timer, problem.

  • Thread starter Thread starter Imogen
  • Start date Start date
I

Imogen

Guest
I have created a UDP sockets class. But theres a fundamental flaw in the class.

When the UDPclient is listening for a reply from the server it hangs my program if no reply is sent. I got over this by running the udpsend in another thread. Great but now I have a thread that needs killing. I thought of using the timer the kill the thread if it didnt repond in the alloted time, but apparently timers only work in a single threaded environment.

Has anyone got any suggestions of how to get around this problem?

Strange to think that with VB6 this task was done easily within 5 mins (this is my 3rd day in VB.NET!

Imogen

PS before you suggest using a TCP client server with a definite connection state, I cant the server is pre-written and I do not have the source code to authority to change it.

Also I could use the VB6 OCX to achieve the desired result... but feel I shouldnt have to, should I be able to do it just as well (if not better in .NET)
 
Do you want the thread to kill itself after a certain time or just exit when the main thread is terminated?

If you just want to prevent the thread keeping your app alive setting the thread's IsBackGround property to true will cause it to be terminate when your app's main thread shuts down.
 
Timers work regardless of the number of threads in your application.

Visual Basic:
'Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf CheckStatus)
      
'Create a timer that waits one second, then invokes every second.
Dim timer As New Timer(timerDelegate, s, 1000, 1000)

To terminate a thread you call:
Visual Basic:
myThread.Abort()
 
Thanks, although since writing my request I have found out that an Asynchronos version of the UDP socket can be created. Has anyone got any information on this ?

Thanks again
 
Back
Top