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 5

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
Implementing BackGroundWorker Events
Here's how the sample application implements the BackgroundWorker's events. The DoWork event shown finds the local fixed drives and directories, and drives the recursive search.

Private Sub searchFolders_DoWork( _ ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles searchFolders.DoWork Dim obj As System.ComponentModel.BackgroundWorker _ = CType(sender, System.ComponentModel. _ BackgroundWorker) Dim textToSearch As String = CType( _ e.Argument, String) 'use generics Dim drives As System.Collections.Generic. _ ReadOnlyCollection(Of DriveInfo) = _ FileSystem.Drives ' recurse and find the number of files so you ' can report progress complete later For Each drive As DriveInfo In drives If drive.DriveType = DriveType.Fixed Then 'first level For Each subdirectory As DirectoryInfo In _ drive.RootDirectory.GetDirectories SetTotalFiles(subdirectory, textToSearch) Next End If Next 'approx total files is now known 'drive level For Each value As DriveInfo In drives If value.DriveType = DriveType.Fixed Then Dim drive As DirectoryInfo = _ value.RootDirectory 'first level For Each subdirectory As DirectoryInfo In _ drive.GetDirectories Dim percentageComplete As Integer = 0 If Not obj.CancellationPending Then SearchDirectoryRecursive(obj, e, _ subdirectory, textToSearch) Else e.Cancel = True End If Next End If Next e.Result = "Process completed." End Sub Private totalfiles As Decimal = 0 Private filesdone As Decimal = 0 Private Sub SetTotalFiles(ByVal directoryInfo _ As DirectoryInfo, ByVal search As String) ' recursively search...and set the totalfiles Try totalfiles = totalfiles + _ directoryInfo.GetFiles.Length For Each info As DirectoryInfo In _ directoryInfo.GetDirectories() totalfiles = totalfiles + _ info.GetFiles.Length For Each subinfo As DirectoryInfo In _ info.GetDirectories() SetTotalFiles(subinfo, search) Next Next Catch ex As System.UnauthorizedAccessException 'ignore this type of error End Try End Sub

The DoWork event provides a DoWorkEventArgs instance as an argument. Table 3 shows three important properties of the DoWorkEventArgs class.

Table 3. DoWorkEventArgs Properties: The table lists the three most important properties of the DoWorkEventArgs class.

e.Argument



Defined as an Object so it has the flexibility to accept any required type—String, Array, Hashtable XMLNode, etc. You set this by passing the parameter while calling the RunWorkerAsync method from the client. The sample code just sends the user-entered search string.

e.Cancel

A critical property that lets you cancel the pending task. When set to True it cancels the current task and the context moves to the RunWorkerCompleted event. You use this in conjunction with the CancellationPending property to check whether the caller has issued a cancellation request.

e.Result

A way to communicate the status or result to the RunWorkerCompleted method. The sample sets a string value that the UI later displays as a result message.


The code shown above fills the drives collection with a list of all drives as returned by the operating system. Note that it uses the new .NET 2.0 syntax for generics—creating a collection type that holds only DriveInfo instances. Then, it iterates through the fixed drives to get their base directories using the GetDirectories method. Next, it loops through each directory using a recursive function named SetTotalFiles. This function retrieves the total number of files to search, which is important for determining or displaying the search percentage complete.



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