Solved

vb.net multithreading

Posted on 2011-09-06
11
303 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 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