By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

vb .net suspend/resume Thread

P: 1
Since from .net 2.0 thread suspend/resume don't work, i have search the internet for another way for manage threads, but now i have a project with a single thread and i need to pause/resume but no method apply here.
Inside my thread is Do Loop that search the internet and show's me the results on a list box array.
I need to pause this function, and when i resume it to search from where it was, i tried many ways and one of them is Monitor Pulse.
Expand|Select|Wrap|Line Numbers
  1. Private _pause As Boolean = False
  2. Private _threadLock As New Object()
  3.  
  4.    Private Sub PauseThread()
  5.         _pause = True
  6.     End Sub
  7.  
  8.     Private Sub ResumeThread()
  9.         _pause = False
  10.         SyncLock _threadLock
  11.             Monitor.Pulse(_threadLock)
  12.         End SyncLock
  13.     End Sub
  14.  
  15.     Private Sub method(ByVal instr As String)
  16.  
  17.         Do
  18.             While True
  19.  
  20.                 If _pause = True Then
  21.                     SyncLock _threadLock
  22.                         Monitor.Wait(_threadLock)
  23.                     End SyncLock
  24.                 ElseIf _pause = False Then
  25.         'Do work    
  26.  
  27.                 End If
  28.  
  29.             End While
  30.         Loop
  31.     End Sub
My problem is when i "suspend" the thread and resume it the thread starts from beginning, the same thing apply if the search is over, same thing it starts from beginning because i put the loop at the beginning of function.
I also tried to Do Loop the "work" bu then i cannot suspend because this means the search or loop it needs to be completed for being "suspended", but the same thing apply here, if i "suspend" and "resume" it it starts from the begginging Monitor.pulse will restarts the thread, it doesn't allow to resume the thread from where it suspend.
I want to know another way that may work for my issue, please help me, Regards Yuri.
Apr 2 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.