Solved

vb.net multithreading

Posted on 2011-09-06
11
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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