Solved

vb.net multithreading

Posted on 2011-09-06
11
294 Views
Last Modified: 2012-05-12
Hi All!

I have to run a very time consumming process and I thought about putting that piece of code inside a backgroundworker. Is this the right way to do multithreading?

Also, how do I make the code inside the backgroundworker run?

Thanks
0
Comment
Question by:pvg1975
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 36491114
If the process is a linear process, there really is no way to multi-thread the process.
If the process has multiple tasks which can be run in parallel, each of these processes should be set as a backgroundworker thread.

A decent example of using BackgroundWorker can be found at
http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx#Y126
0
 

Author Comment

by:pvg1975
ID: 36491166
I saw those examples, but when I try to call the code as:

backgroundworker1.dowork()

I get the following error:

Public Event DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

I just need to know how to call and run the code inside backgroundworker1

Thanks
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 250 total points
ID: 36491220
Declare your worker:
Private WithEvents MyBackgroundWorker As New System.ComponentModel.BackgroundWorker()

Start Worker
MyBackgroundWorker.RunWorkerAsync(myParam)

DoWork Mehtod:
Private Sub MyBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles MyBackgroundWorker.DoWork
            'DoStuff
End Sub

Completed Callback:
Private Sub MyBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles MyBackgroundWorker.RunWorkerCompleted
            'DoOtherStuff
End Sub
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 250 total points
ID: 36491225
Hello, you must call the method RunWorkerAsync()

backgroundworker1.RunWorkerAsync()

Open in new window

0
 

Author Comment

by:pvg1975
ID: 36491246
Thank you all for the answers :)))
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36491283
Not even an assist?
0
 

Author Comment

by:pvg1975
ID: 36491334
I know I screwed it up after I assigned the points :(

Im sorry about that Stick, I clicked on the email and toke me straight to yv989c answer.

Will press more attention next time.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36491388
You can always select to "Request Attention" and have the split performed by a moderator.
0
 

Author Comment

by:pvg1975
ID: 36491398
Done :)
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36491820
Oh, I just answered to your question based on your error, but strickdd gave a detailed information about how to work with the BackgroundWorker class, answering that too, and he pressed the submit button before me ;)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question