Solved

vb.net multithreading

Posted on 2011-09-06
11
277 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now