Question about Threading
-
In my application I have a process that can take quite a bit of time to run (so I have it in it's own thread so I can display progress to the user). In the event that a 2nd process starts I need to pause the thread, then restart it once the 2nd process is complete. When I use Thread.Resume to restatt it it displays a warning
Public Sub Resume()' is obsolete: 'Thread.Resume has been deprecated.
Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.What are the acceptable replacements for .Resume as I have Google'd it and cant find the answer
"It's only that urgent if you have to pee." Dave Kreskowiak
-
In my application I have a process that can take quite a bit of time to run (so I have it in it's own thread so I can display progress to the user). In the event that a 2nd process starts I need to pause the thread, then restart it once the 2nd process is complete. When I use Thread.Resume to restatt it it displays a warning
Public Sub Resume()' is obsolete: 'Thread.Resume has been deprecated.
Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.What are the acceptable replacements for .Resume as I have Google'd it and cant find the answer
"It's only that urgent if you have to pee." Dave Kreskowiak
Psycho-*Coder*-Extreme wrote:
I need to pause the thread, then restart it once the 2nd process is complete
How did you "pause" this thread?? I hope you didn't call
Abort()
on it. Once a thread is stopped, that's it, you can't restart it. You have to destroy it and create another one. Your thread should be checking for a flag set by your main thread to tell it to "pause". Once the 2nd process completes, you can reset this flag telling your first thread to resume.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Psycho-*Coder*-Extreme wrote:
I need to pause the thread, then restart it once the 2nd process is complete
How did you "pause" this thread?? I hope you didn't call
Abort()
on it. Once a thread is stopped, that's it, you can't restart it. You have to destroy it and create another one. Your thread should be checking for a flag set by your main thread to tell it to "pause". Once the 2nd process completes, you can reset this flag telling your first thread to resume.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007For pausing it I use
'Put the thread to sleep for as long as I need it
Thread.Sleep(System.Threading.Timeout.Infinite)
'Set the flag
bIsPaused = TrueThen to resume it I use
'Check the status of the thread
If thrdAgentStatus.ThreadState And ThreadState.Unstarted <> 0 And Not bIsPaused Then
thrdAgentStatus.Start()
Else
thrdAgentStatus.Resume()
End IfMy problem is that the .Resume gives a warning that it is depreciated and I was trying to find an alternative so I can get rid of this warning.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
-
For pausing it I use
'Put the thread to sleep for as long as I need it
Thread.Sleep(System.Threading.Timeout.Infinite)
'Set the flag
bIsPaused = TrueThen to resume it I use
'Check the status of the thread
If thrdAgentStatus.ThreadState And ThreadState.Unstarted <> 0 And Not bIsPaused Then
thrdAgentStatus.Start()
Else
thrdAgentStatus.Resume()
End IfMy problem is that the .Resume gives a warning that it is depreciated and I was trying to find an alternative so I can get rid of this warning.
"Okay, I give up: which is NOT a real programming language????" Michael Bergman
Psycho-*Coder*-Extreme wrote:
'Put the thread to sleep for as long as I need itThread.Sleep(System.Threading.Timeout.Infinite)'Set the flagbIsPaused = True
OK. That's not going to work. The flag is never set because the thread sleeps, or Blocks, immediately. Start and Resume will not "wake up" a blocked thread. You have to call the Thread object's
Interrupt
method to unblock a blocked thread.Dim oThread As System.Threading.Thread oThread = New Thread(AddressOf Me.Fill) oThread.Start() oThread.Sleep(System.Threading.Timeout.Infinite) Dim retValue As MsgBoxResult = MsgBox("Wake Thread?") If retValue = MsgBoxResult.Yes Then oThread.Interrupt() End If
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007