[2005] Threading and Synchronization
Hi Everybody!
I am still learning how to take full advantage of threading in VB.Net. There seems to be a swamp of information out there that just seems to confuse me more.
My specific question relates to synchronization. I have some software that performs a lengthy file IO operation (Threads and IO :confused: ). During this operation the GUI becomes unresponsive because it is tied up. I want to pass the file operation off to separate thread and allow to main GUI thread to keep pumping messages and responding. Many options exist, I have been using ThreadPool.QueueUserWorkItem in the past with great success.
My problem is that I want to wait for that thread to complete before I continue with further operations. I think I want to use ManualResetEvent but am unsure how to apply it.
Code:
Public Sub DoStuff()
{
Operation1()
ThreadPool.QueueUserWorkItem(addressof FileOperation())
Operation2() //Do not process until worker thread has returned
}
It would also be nice if there is a timeout on such a device. If the file op. fails or something else it would be nice if it timed out and returned anyway.
I have found things like the Mutex and Monitor class but they seem to lock variables. I actually need to halt further processing of the main thread's code until my worker has returned.
Just a little lost in massive world of threading support.
Thanks ppl,
Matt.
Re: [2005] Threading and Synchronization
The whole point of threading is so that you don't lock the main threads code while you're performing some other operation. If you don't want to perform Operation2 until the thread has completed then don't call it in the same method. Call it from a method that gets executed when the worker thread has completed. Note that the BackgroundWorker component is useful in this regard because it will raise its RunWorkerCompleted event when it's done.
Note also that threads don't "return". They just complete their processing. If you want your thread to complete if it has been running for a certian period of time then write that into the code. Have it note the time when it starts and then have it intermittently check the time and complete its processing if it goes over. Alternatively, if you're using a BackgroundWorker you can have the main thread call its CancelAsync method if it goes over time.
Re: [2005] Threading and Synchronization
Sorry JM, I haven't explained myself fully.
I don't want to lock the GUI thread, it has a progress bar that I want updated. Problem is if I use the main thread for the file operation the progress bar locks and the GUI becomes unresponsive.
I want another thread to do the file operation and use Invoke to report progress back the GUI thread.
So I dont actually want to lock the main thread, I just dont want it to continue processing down to "Operation2()" until the FileOp is complete.
I saw a small example on CodeProject that simply sat in a loop sleeping and checking the state of the thread. Maybe this is the answer - give the GUI thread something lightweight to do until the other one finishes.
Some pseudo code...
Code:
While(bGo)
{
Threading.Sleep(1000)
If (FileThread has closed) or (Timeout Expired) then
bGo=False
else
bGo=True
end if
Application.DoEvents() 'Ensure the prog bar is updated
}
There must be a better way...
Matt
Re: [2005] Threading and Synchronization
I've told you what the better way is already.
jmcilhinney Code:
If you don't want to perform Operation2 until the thread has completed then don't call it in the same method. Call it from a method that gets executed when the worker thread has completed.