Browse DevX
Sign up for e-mail newsletters from DevX


New Features in Visual Studio 2005 Windows Forms : Page 6

Updated IDE features, new Menu, Toolstrip and multimedia controls, support for background threading, and a new deployment technology called ClickOnce aimed squarely at reducing the number of Web-based intranet applications are among the new capabilities in Visual Studio 2005.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

BackgroundWorker Component
Consider the words "multithreaded programming." For some people these words conjure up images of late-night coding sessions hunched over .NET Framework books or sifting through the Help file trying to get a solid grip on how to run multiple tasks on separate execution threads. That's not how I want to spend my nights. I prefer an activity you may have heard of called sleep.

Fortunately I can get plenty of sleep because of the introduction of the new Background Worker class. The BackgroundWorker component makes it easy for time-consuming operations, the type of operations that can cause your user interface to act as though it's stuck in mud, to run on a separate, dedicated thread. Examples of this type of operation are file downloads, database transactions, connecting to a Web service, or working with large text files.

You can drag a BackgroundWorker component from the Toolbox onto a form or you can create it programmatically. The RunWorkerAsync method begins the execution of the background operation being performed. The RunWorkerSync method fires the DoWork event on a background thread and it is in the handler for this event that you code the operation to take place. If the WorkerReportsProgress property is set to True (see Table 7), the ReportProgress method can be called while the operation is underway. This method raises the ProgressChanged event which is handled on the form to keep the user informed, typically through the use of a progress bar.

Table 7: Use these properties to configure the BackgroundWorker.




Returns a Boolean value indicating whether the application has requested cancellation of an operation.


Returns a Boolean value indicating whether an asynchronous operation is running.


Returns or sets a Boolean value indicating whether the BackgroundWorker object can report progress updates.


Returns or sets a Boolean value indicating whether the BackgroundWorker object supports asynchronous cancellation.

If the WorkerSupportsCancellation property is set to True, the CancelAsync method can be called to cancel the background operation. The RunWorkerCompleted event fires when the background operation has completed, has been cancelled, or has raised an exception (see Table 8).

Table 8: The BackgroundWorker supports a number of methods and events.




Cancels a pending background operation.


Fires the ProgressChanged event.


Begins execution of a background operation.




Begins the execution of a background process.


Fires when ReportProgress is executed.


Fires when the background operation completes, has been canceled, or has raised an exception.

Figure 14. Background Processing: This sample BackgroundWorker Component form contains all the pieces of a typical BackgroundWorker form.
The BackgroundWorker Component Test form (see Figure 14) uses a BackgroundWorker component to perform a calculation on a separate thread. The "Start Operation" button launches the thread by calling the RunWorkerAsync method, passing in the length of time the thread should wait, simulating a lengthy process. The "Cancel Operation" button calls the CancelAsync method and provides the user with the ability to cancel the operation. The "Operation Busy?" Button calls the IsBusy method and provides the user with the ability to determine if the process is still underway.

The BackgroundWorker1_DoWork sub handles the DoWork event fired when the RunWorkerAsync method is called. Reviewing the BackgroundWorker1_DoWork procedure code in Listing 1, you can see that the ReportProgress method is called inside a processing loop. The BackgroundWorker1_ProgressChanged sub handles the ProgressChanged event raised and updates the progress bar.

Once the DoWork method is complete, the BackgroundWorker1_RunWorkerCompleted sub handles the RunWorkerCompleted event that is fired and the form is updated to reflect the outcome of the operation. See Listing 1 for the complete code listing for the form.

ClickOnce Deployment
One of the most attractive features of a Web-based application is the ease in which you can deploy and update it. All a user has to do is point their browser at the correct URL and away they go.

With .NET 2.0 comes a new deployment technology called ClickOnce aimed squarely at reducing the number of Web-based intranet applications being developed in favor of developing smart client applications. ClickOnce can create a setup program that can be distributed and installed via traditional measures (a CD), launched over a network, or launched through a Web page. You can also configure ClickOnce applications to automatically check for updates each time the application executes.

I hope this article has given you a taste of some of the new and exciting Windows Forms features that Microsoft has added to Visual Studio 2005. You can learn more about Windows Forms development by reading the Data Binding and ClickOnce articles in this issue. I hope this article has peaked your curiosity and motivated you to investigate what else Visual Studio 2005 has to offer. As always, I welcome your comments and look forward to hearing from you.

Jim Duffy is founder and president of TakeNote Technologies, an award-winning training and software development company. He has a BS degree in Computer and Information Systems and over 19 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications. Jim, a Microsoft MVP award recipient, is a popular speaker at regional user groups and developer conferences. He is also a co-host of Computers 2K4, a call-in radio show on WRBZ (AM 850), 850 The Buzz, in Raleigh, NC. Jim is also the author of TakeNote's popular VFP database auditing product, FoxAudit. In addition to his duties with TakeNote, Jim is also the VP of Sales & Marketing for Red Matrix Technologies, a SQL Server tools vendor who's SQLAudit product adds complete audit trail support for SQL Server databases. Additional information about Jim, TakeNote Technologies, and Red Matrix Technologies can be found at www.takenote.com.
Thanks for your registration, follow us on our social networks to keep up-to-date