0

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.

user1665232
  • 221
  • 5
  • 15
  • We need more informations/code to better understand where the problem is: in any case I don't think the solution may be to clean the stack (is never a solution), you need to understand what is impiling up into the stack and solve the problem at it's root – AlexF Apr 14 '14 at 07:53
  • There is no particular reason why running a subroutine, finishing it, and then running the same one again should cause a stackoverflow. I think your problem lies elsewhere, in code that you have not shown us. – merlin2011 Apr 14 '14 at 07:55
  • I added some more code. Is this enough or do you need more details? – user1665232 Apr 14 '14 at 08:38

2 Answers2

1

Your method connect is recursive without any condition under which it will return.

Each call to a function or method will make use of some stack space, and each thread only has so much stack space.

If you want connect to loop for ever just use an infinite loop (but consider how the lop will exit when you want to stop the process).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • So you mean, i could just start connect() as a background thread, and put an endless loop in this method? Without calling another method all the time? – user1665232 Apr 14 '14 at 09:03
1
Public Sub connect()
   ...
   connect()
End Sub

So which part of the obvious stack overflow is unclear to you? You are calling connect recursively w/o any criteria. It will stack overflow, guaranteed. Your Sleep() only delays the inevitable. Threading has nothing to do with the problem. You may want to do somehting like a loop instead:

while(true)
 connect_loop()
end

(or whatever is the VB syntax for a while loop).

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • So you mean, i could just start connect() as a background thread, and put an endless loop in this method? Without calling another method all the time? – user1665232 Apr 14 '14 at 09:01
  • Thanks! Do you think it could raise any error or exception, using the while(true) loop? I'm a bit afraid of overloading my application or somethin else? Is there anything bad about using a While(true) loop to make sure, that my application is always connected or reconnect itself? And do i need the sleep timer? – user1665232 Apr 14 '14 at 09:16