Question Help with threading

Messi10

New member
Joined
Apr 3, 2012
Messages
3
Programming Experience
Beginner
Hi,

I'm very much a newbie when it comes to programming, hopefully someone can help me out here please :( I have created an application which uses threading and processes my problem is I use the thread.waitfor method before my application can proceed with the next part of the coding but my problem is that my application freezes until the thread.waitfor has completed.

VB.NET:
Public Class Form1

    Private Shared ResetE As New AutoResetEvent(False)

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        startthread()
        ResetE.WaitOne()
       ' then my next bit of coding
    End Sub

    Public Sub startthread()
        Dim processThread As New Threading.Thread(AddressOf StartProcess)
        processThread.IsBackground = True
        processThread.Start()
      
    End Sub

  Public Sub StartProcess()


         'Open Process coding
        ResetE.Set() 
         'Close process coding


    End Sub

Now what happens is my application just freezes until it reaches .set, hopefully someone can help me.
 
Last edited:
The UI is freezing because you're telling it to. You start the mew thread and then you tell the UI thread to wait until it's finished. The whole point of multi-threading in a GUI app is that the UI does NOT wait for the background task to finish. The UI starts the background and then just carries on with its business. If you want to do something when that task completes then it's up to the background task itself to provide notification. The simplest way to do this is with a BackgroundWorker. Call RunWorkerAsync and then handle RunWorkerCompleted. If you don't want the user to be able to things in between then you need to disable the appropriate controls, e.g. on the Click of a Button, disable the Button and call RunWorkerAsync, then enable the Button again in the RunWorkerCompleted event handler.
 
It is perfectly valid to synchronize background tasks using a wait handle like AutoResetEvent, but then you must do that synchronization a secondary thread and not block the UI thread.
BackgroundWorker as mentioned may be easier to use in GUI environment.
You can also look into the Task Parallel Library that was added to .Net 4, and has many new features for managing parallel tasks.
I'm on VS2010 Net Framework 4
Do change your forum profile if that is the default platform.
 
Thanks guys,

I had a look at the background worker and I believe this is the route I shall be going down. JMcilhinney I think that is the best way, because what I can do is run my code and then have my button disabled and once the application is back to life then the button can be enabled.

So using a background worker I have got to the following but I cant see a RunWorkerCompleted handle??

Also as stated below I use a background worker but it still hangs?

VB.NET:
Public Class Form1

    Private WithEvents BGWorker As System.ComponentModel.BackgroundWorker = Nothing
    Private Shared ResetEvent As New AutoResetEvent(False) 

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Start()
       
       ' then my next bit of coding
    End Sub

    Public Sub  Start()
        'Start the BackGroundWorker  
        BGWorker = New System.ComponentModel.BackgroundWorker
        BGWorker.WorkerSupportsCancellation = True
        BGWorker.RunWorkerAsync()
        ResetEvent.WaitOne()
    End Sub
 Private Sub BGWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWorker.DoWork
 
        ' Create my process
        ' Do my command
        ResetEvent.Set()
        ' Close my process
       
    End Sub
 
 Public Sub Cancel()  
        'Stop the thread  
        If BGWorker.IsBusy Then  
            BGWorker.CancelAsync()  
            BGWorker = Nothing  
        End If  
        'Kill the process  
        If myProcess IsNot Nothing Then myProcess.Kill()  
    End Sub
 
You're not supposed to be using a WaitHandle at all. That WaitOne is what was freezing your UI before and it still is. Get rid of it altogether. You don't need, or want, to explicitly wait. As I already stated earlier, the UI threads starts the background task, in this case by calling RunWorkerAsync, and then just gets on with its business. The background work is done in DoWork event handler and then, when the DoWork event handler completes, the RunWorkCompleted event is raised. That is raised on the UI thread, allowing you to update the UI after completion of the background task. Here's one I prepared earlier:

Using the BackgroundWorker Component
 
Back
Top