+ Reply to Thread
Results 1 to 8 of 8

Thread: Multi threading in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Multi threading in VB.NET

    Usually when you're code is doing anything slow, everything has to wait for it to get finished before they can do anything, this will happen when our application is single threaded. To solve this we can add a backgroundworker object, you can found them in the toolbox. If you want to follow the tutorial by doing the same as I'll tell you, then create a background worker called "Worker1".

    In this example, We want to be able to report progress and cancel the worker when we want, then we want to start it. To do this we do like this:


    Code:
        Private Sub frmMain_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load
            Worker1
    .WorkerReportsProgress True
            Worker1
    .WorkerSupportsCancellation True
            Worker1
    .RunWorkerAsync()
        
    End Sub 
    So now the working will start to work, so now it will call its DoWork event which will be in another thread so it won't interrupt anything else. Since this is only examples to teach you, this thread will loop through an empty for loop one billion times, just to let it do anything that takes some time. For me it takes 4 seconds but If you have a slow computer you can lower the amount of times the loop will loop:


    Code:
        Private Sub Worker1_DoWork(ByVal sender As System.ObjectByVal e As System.ComponentModel.DoWorkEventArgsHandles Worker1.DoWork
            
    For As Long 1 To 1000000000

            Next
        End Sub 

    Normally when an application is doing something which takes some time to finish and you try to interact with it by for example clicking on it, it will stop to respond, which isn't the case now since the time taking action is done in another thread.


    When the backgroundworking is done we often wants to do something, we can then use the RunWorkerCompleted event to do this. In this example we'll just show a messagebox saying it's done:


    Code:
        Private Sub Worker1_RunWorkerCompleted(ByVal sender As ObjectByVal e As System.ComponentModel.RunWorkerCompletedEventArgsHandles Worker1.RunWorkerCompleted
            MessageBox
    .Show("Worker1 is done!")
        
    End Sub 
    So why don't we just do this in the end of the DoWork event? The reason is that that event is working in another thread and therefor can't access things from another thread, you can still access variables from outside the special thread and the above example would work fin inside the other thread but if we for example want to alter anything on the form we can't, since that is created by another thread. So if we added this at the end of DoWork(inside the out other thread):

    Code:
    Me.Text "Worker1 is done!" 
    we would receive this error:

    Code:
    Cross-thread operation not valid: Control 'frmMain' accessed from a thread other than the thread it was created on.
    To solve this we do as above, using the RunWorkerCompleted event since that will be run in our main thread and can therefor alter for example our main form.



    In the same way we maybe want to alter something from another thread inside our background worker, to do this we have to use the ProgressChanged event. If we alter the DoWork event to this:


    Code:
        Private Sub Worker1_DoWork(ByVal sender As System.ObjectByVal e As System.ComponentModel.DoWorkEventArgsHandles Worker1.DoWork
            
    For As Long 1 To 1000000000
                Select 
    Case i
                    
    Case 1
                        Worker1
    .ReportProgress(0)
                    Case 
    250000000
                        Worker1
    .ReportProgress(25)
                    Case 
    500000000
                        Worker1
    .ReportProgress(50)
                    Case 
    750000000
                        Worker1
    .ReportProgress(75)
                    Case 
    1000000000
                        Worker1
    .ReportProgress(100)
                
    End Select
            Next
        End Sub 
    We'll then on some values(1, 250 millions, 500 millions, 750 millions and 1 billion) report the progress (0%, 25%, 50%, 75% and 100%). This can be used in the ProgressChanged event like this:


    Code:
        Private Sub Worker1_ProgressChanged(ByVal sender As ObjectByVal e As System.ComponentModel.ProgressChangedEventArgsHandles Worker1.ProgressChanged
            Me
    .Text e.ProgressPercentage "% done"
        
    End Sub 
    So then we'll get the Progress done outside the special thread so we can use it in our main thread.




    That was everything for this tutorial about multi threading, hope you enjoyed it.
    Last edited by James.H; 03-05-2010 at 10:04 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Multi threading in VB.NET

    Nicely done. How often do you use these workers in your code?
    +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Multi threading in VB.NET

    I actual don't use them that often because I don't make that kind of programs so often. But from time to time I use them.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Multi threading in VB.NET

    Threading... gotta love/hate it +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: Multi threading in VB.NET

    Great stuff!

  7. #6
    Join Date
    Mar 2010
    Location
    Melbourne, Australia
    Posts
    17
    Blog Entries
    1
    Rep Power
    8

    Re: Multi threading in VB.NET

    Great info, helped me a bit in a few issues i'm having.
    The only issues i have with multi-threading is you have to have the end result data carrier such as a list-box or text-box as a public control :-/
    which makes security fall apart for decompliers that try to rip off your software but i sapose you cant avoid that these days.

    Great stuff though, +Rep

  8. #7
    Zizooooo is offline Newbie
    Join Date
    Jul 2009
    Posts
    14
    Rep Power
    0

    Re: Multi threading in VB.NET

    I found a simple definition on another site so i prefered to write it here not because this tut is bad " it is really wonderful " but for more details:
    What is Multithreading
    Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time. We are all used to Windows' multitasking abilities, which allow us to execute more than one application at the same time. Just right now I am writing the 14th lesson of the Programmers Heaven's VB.NET School in Microsoft Word, listening to my favorite songs in WinAmp and downloading a new song using the Internet Download Manager. In a similar manner, we may use multithreading to run different methods of our program at the same time. Multithreading is such a common element of today's programming that it is difficult to find windows applications that don't use it. For example, Microsoft Word takes user input and displays it on the screen in one thread while it continues to check spelling and grammatical mistakes in the second thread, and at the same time the third thread saves the document automatically at regular intervals. In a similar manner, WinAmp plays music in one thread, displays visualizations in the second and takes user input in the third. This is quite different from multitasking as here a single application is doing multiple tasks at the same time, while in multitasking different applications execute at the same time.

  9. #8
    polas is offline Newbie
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Re: Multi threading in VB.NET

    Your code doesn't work at all.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. [C]Threading
    By AdvMutant in forum C and C++
    Replies: 2
    Last Post: 12-05-2010, 10:34 PM
  2. Threading in PHP
    By aerosko0315 in forum PHP Development
    Replies: 3
    Last Post: 07-29-2010, 10:46 AM
  3. Threading
    By Grouchotron in forum Java Help
    Replies: 1
    Last Post: 07-22-2010, 06:24 AM
  4. Multi-Threading for Web Development
    By rsnider19 in forum General Programming
    Replies: 1
    Last Post: 09-01-2009, 09:24 AM
  5. Help on bluetooth and threading
    By alibahaloo in forum C and C++
    Replies: 11
    Last Post: 08-12-2009, 07:10 PM

Tags for this Thread

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