It is important to note that in this method (which was invoked by the DoWork
event), you cannot directly access Windows controls because they are not thread-safe. Trying to do so will trigger a runtime error, a useful feature in Visual Studio 2008.
The ProgressChanged
event is invoked whenever the ReportProgress()
method is called. In this case, you use it to update the progress bar. To generate the event handler for the ProgressChanged
event, switch to design view and look at the properties of the BackgroundWorker
component. In the Properties window, select the Events icon and double-click the ProgressChanged
event (see Figure 10-15).
Figure 10-15
Code the event handler for the ProgressChanged
event as follows:
private void backgroundWorker1_ProgressChanged(
object sender, ProgressChangedEventArgs e) {
//---updates the progress bar and label control---
progressBar1.Value = e.ProgressPercentage;
lblResult.Text = e.ProgressPercentage.ToString() + "%";
}
Now double-click the RunWorkerCompleted
event to generate its event handler:
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e) {
if (e.Error != null) MessageBox.Show(e.Error.Message);
else if (e.Cancelled) MessageBox.Show("Cancelled");
else {
lblResult.Text = "Sum of 1 to " + txtNum.Text + " is " + e.Result;
}
btnStart.Enabled = true;
btnCancel.Enabled = false;
}
The RunWorkerCompleted
event is fired when the thread ( SumNumbers()
, in this case) has completed running. Here you print the result accordingly.
Finally, when the user clicks the Cancel button, you cancel the process by calling the CancelAsync()
method:
private void btnCancel_Click(object sender, EventArgs e) {
//---Cancel the asynchronous operation---
backgroundWorker1.CancelAsync();
btnCancel.Enabled = false;
}
To test the application, press F5, enter a large number (say, 9999999), and click the Start button. The progress bar updating should begin updating. When the process is complete, the result is printed in the Label
control (see Figure 10-16).
Figure 10-16
This chapter explains the rationale for threading and how it can improve the responsiveness of your applications. Threading is a complex topic and you need to plan carefully before using threads in your application. For instance, you must identify the critical regions so that you can ensure that the different threads accessing the critical region are synchronized. Finally, you saw that Windows Forms controls are not thread-safe and that you need to use delegates when updating UI controls from another thread.
Chapter 11
Files and Streams
At some stage in your development cycle, you need to store data on some persistent media so that when the computer is restarted the data is still be available. In most cases, you either store the data in a database or in files. A file is basically a sequence of characters stored on storage media such as your hard disks, thumb drives, and so on. When you talk about files, you need to understand another associated term — streams. A stream is a channel in which data is passed from one point to another. In .NET, streams are divided into various types: file streams for files held on permanent storage, network streams for data transferred across the network, memory streams for data stored in internal storage, and so forth.
With streams, you can perform a wide range of tasks, including compressing and decompressing data, serializing and deserializing data, and encrypting and decrypting data. This chapter examines:
□ Manipulating files and directories
□ How to quickly read and write data to files
□ The concepts of streams
□ Using the BufferedStream
class to improve the performance of applications reading from a stream
□ Using the FileStream
class to read and write to files
□ Using the MemoryStream
class to use the internal memory store as a buffer
□ Using the NetworkStream
class for network programming
□ The various types of cryptographic classes available in .NET
□ Performing compressions and decompression on streams
□ Serializing and deserializing objects into binary and XML data
Working with Files and Directories
The System.IO
namespace in the .NET Framework contains a wealth of classes that allow synchronous and asynchronous reading and writing of data on streams and files. In the following sections, you will explore the various classes for dealing with files and directories.
Remember to import the System.IO
namespace when using the various classes in the System.IO
namespace.
The .NET Framework class library provides two classes for manipulating directories:
□ DirectoryInfo
class
□ Directory
class
The DirectoryInfo
class exposes instance methods for dealing with directories while the Directory
class exposes static methods.
DirectoryInfo Class
The DirectoryInfo
class provides various instance methods and properties for creating, deleting, and manipulating directories. The following table describes some of the common methods you can use to programmatically manipulate directories.
Method |
Description |
Create |
Creates a directory. |
CreateSubdirectory |
Creates a subdirectory. |
Delete |
Deletes a directory. |
GetDirectories |
Gets the subdirectories of the current directory. |
GetFiles |
Gets the file list from a directory. |
And here are some of the common properties:
Properties |
Description |
Exists |
Indicates if a directory exists. |
Parent |
Gets the parent of the current directory. |
FullName |
Gets the full path name of the directory. |
CreationTime |
Gets or sets the creation time of current directory. |
Refer to the MSDN documentation for a full list of methods and properties.
To see how to use the DirectoryInfo
class, consider the following example:
static void Main(string[] args) {
string path = @"C:\My Folder";
DirectoryInfo di = new DirectoryInfo(path);
try {
//---if directory does not exists---
if (!di.Exists) {
//---create the directory---
di.Create(); //---c:\My Folder---
//---creates subdirectories---
Читать дальше