Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Better UIs with the Asynchronous Support in .NET 2.0  : Page 7

Good user interfaces let users keep working as seamlessly as possible while an application performs long background processing tasks. While .NET 1.0 certainly simplified the process of launching and managing multiple threads in Windows applications, you had to write much of the infrastructure yourself. In contrast, .NET 2.0 adds direct framework support for asynchronously fetching data and performing background tasks.


advertisement
Displaying Search Results and Progress
When a file matches the search criteria, the BackGroundWorker class invokes the registered FoundInfo delegate, which fires the FileInformation method on the form. The FileInformation method receives a FileInfo object. Because FileInformation executes in the worker thread context, it calls the Form's Invoke method to delegate the call to the Form's thread context. The Form invokes the SetListValue method (now on the proper thread), which adds the file's information to the ListView control. It also reads the BackgroundWorker's progress and sets the lblProgress (label) and pgBar(progress bar) values. In other words, on each callback the code updates the progress value and adds new file information.

Private Delegate Sub SetValue(ByVal file As Object) Public Sub FileInformation(ByVal file As FileInfo) Try Dim handle As New SetValue( _ AddressOf Me.SetListValue) Dim param1 As Object() = New Object() {file} Me.Invoke(handle, param1) Catch ex As Exception objContentsearcher.Worker.CancelAsync() End Try End Sub Sub SetListValue(ByVal fle As Object) Try Dim file As FileInfo = CType(fle, FileInfo) Dim lv1 As ListViewItem = New _ ListViewItem(file.Name) Dim lv2 As ListViewItem = New _ ListViewItem(file.Directory.FullName) lstResult.Items.Add(lv1) lstResult.Items.Add(lv2) Dim progressCompleted As Short = _ objContentsearcher.Progress lblProgress.Text = "Progress completed = " + _ progressCompleted.ToString + "%." pgBar.Value = progressCompleted Catch ex As Exception objContentsearcher.Worker.CancelAsync() End Try End Sub

Figure 5. Sample Search Results: The figure shows matching file results in the sample application form while performing a search for files whose names contain the search text "word." The progress bar shows that the application has completed approximately one-third of the search.
At the end of the search, the BackgroundWorker makes a call to the method registered as the ProcessCompleted delegate—in this case, the ProcessComplete method shown below, passing either the error, success, or user cancellation message to display on the form.

Private Sub ProcessComplete(ByVal value As String) lblProgress.Text = value pgBar.Visible = False isWorkerStarted = Not isWorkerStarted End Sub

Figure 5 shows how the search application looks after performing a search.

Fetching data asynchronously can go a long way toward improving the responsiveness of your Windows Forms and SmartClient applications; however, don't simply assume that the Asynchronous model is always the best approach. Frequently, the synchronous model is a much better choice. It is nice to have the choice, though, and .NET 2.0 simplifies the programming tasks, no matter which approach you decide to use. The .NET 2.0 framework extends asynchronous support far more widely than discussed in this article. For example, if the built-in BackgroundWorker doesn't meet your needs, you can create custom classes that support asynchronous operations. You should explore the controls you use to see which ones expose this pattern.



Vishnu Prasad H is a Microsoft Visual C# MVP working as Consultant at Capco IT Services India Ltd, Bangalore. He has four years of practical experience and specializes in Microsoft-based technologies like .NET, COM/COM+, and SQL Server.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date