Results 1 to 6 of 6

Thread: Help with threading

  1. #1
    Messi10 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Apr 2012
    Posts
    3
    Reputation
    0

    Help with threading

    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.

    Code:
    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 by Messi10; 05-12-2012 at 5:10 PM. Reason: I'm on VS2010 Net Framework 4

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    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.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    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.

  4. #4
    Messi10 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Apr 2012
    Posts
    3
    Reputation
    0
    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?

    Code:
    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

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    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

  6. #6
    Messi10 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Apr 2012
    Posts
    3
    Reputation
    0
    Thanks Jim thats helped me a great deal, they should have you in the hall of fame..
    Last edited by Messi10; 05-13-2012 at 6:13 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking