Start a process in the Background in Visual Basic 2010
If your Visual Basic.net application has some time consuming process, and you don't want to allow the user interface to be delayed while waiting for this process, you can decide start as a background process. How you start this process asynchronous is quite easy, if you make good use of the building materials Microsoft offers you in Visual Basic.
Add BackgroundWorker Component to your form
Step 1 is to add a BackgroundWorker to your form. You can find this object in your toolbox in Visual Basic 2010 in the Components area. The default name for an added Backgroundworker Component is BackgroundWorker1. If you don't break company rules concerning naming conventions, you can keep this name.
Insert Code Snippet for Asynchronous Method Call
The second step is to insert in your code some methods, using a Code Snippet. Make sure to place your cursor inside the Class Definition of the form, but outside any sub or function definition. Right click and choose option Insert Snippet.... Click on Application - Compiling, Resources and Settings, and choose the snippet Make an Asynchronous Method Call.
You'll get three methods: startBackgroundTask(), BackgroundWorker1_DoWork() and BackgroundWorker1_RunWorkerCompleted.
startBackgroundTask()
Call this method to start the background task asynchronous.
BackgroundWorker1_DoWork()
In this method you write the code that will be run asynchronous.
BackgroundWorker1_RunWorkerCompleted()
Use this event to write code you want to run when the time consuming background process is ended.
|