vb.net multithreading

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
pvg1975Asked:
Who is Participating?
 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Hello, you must call the method RunWorkerAsync()

backgroundworker1.RunWorkerAsync()

Open in new window

0
 
WalkaboutTiggerCommented:
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
 
pvg1975Author Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
strickddConnect With a Mentor Commented:
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
 
pvg1975Author Commented:
Thank you all for the answers :)))
0
 
strickddCommented:
Not even an assist?
0
 
pvg1975Author Commented:
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
 
strickddCommented:
You can always select to "Request Attention" and have the split performed by a moderator.
0
 
pvg1975Author Commented:
Done :)
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
All Courses

From novice to tech pro — start learning today.