i'm using a background Thread in vb.net to connect/reconnect to a device. It's working fine, but after around 2 hours, the programm is throwing a stackoverflow exception, in the following part of my code:
if connected = True then
Thread.Sleep(500)
Exit sub
endif
After that, i'm calling the Sub again.
I know, i could just set the Sleep-Time to e.g. 1000 ms, but i think this isnt the nicest solution... Would a "Backgroundworker" (using visual studio) be the better solution/ solve my Problem? Or is there a posibility to clean the stack?
Thanks for your help!
EDIT:
Module connection
Public Sub connect()
connect_loop()
connect()
End Sub
Public sub connect_loop()
if connected = True Then
**HERE IS WHERE THE EXCEPTION IS THROWN**
Thread.Sleep(500)
Exit Sub
Endif
'Code for the Connection (ping, open Socket etc.)....
End Sub
End module
The module is started from my main routine as a background thread:
Public background As New Thread(AddressOf connection.connect)
background.IsBackground = True
background.Start()
Do you need the Code for the connection? I didn't wrote it here, because the Exception is not thrown in the real connection part. Also, 2 hours everything works fine.