Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

Example of the BackgroundWorker Class in a WPF Program

Since the BackgroundWorker class is primarily used with GUI programming, the following program shows its use in a simple WPF program rather than the console programs we’ve used throughout the text. WPF is Microsoft’s replacement for the Windows Forms GUI programming framework. For further information about WPF programming, please see my book Illustrated WPF, also published by Apress.

This program produces the window shown on the left in Figure 22-4. When you click the Process button, it starts the background thread, which reports to the main thread every half second and increments the progress bar at the top by 10 percent. At completion, it shows the dialog box on the right of Figure 22-4.

Figure 22-4. The example WPF program using the BackgroundWorker class

To create this WPF program in Visual Studio 2010, do the following:

1.Select the File New Project menu item, which pops up the New Project window.

2.In the pane on the left of the window, open the Installed Templates section, if it’s not already open.

3.Under the C# category, click the Windows entry. This populates the center pane with the installed Windows program templates.

4.Click WPF Application, and then at the bottom of the window enter SimpleWorker in the Name text box. Below that, select a location, and click the OK button.

There are only two files you’ll modify—MainWindow.xaml and MainWindow.xaml.cs. Modify your MainWindow.xaml file to match the following listing:

<Window x:Class="SimpleWorker.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="150 " Width="250">

<StackPanel>

<ProgressBar Name="progressBar" Height="20" Width="200" Margin="10"/> <Button Name="btnProcess" Width="100" Click="btnProcess_Click"

Margin="5">Process</Button>

<Button Name="btnCancel" Width="100" Click="btnCancel_Click" Margin="5">Cancel</Button>

</StackPanel>

</Window>

610

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

Modify your MainWindow.xaml.cs file to match the following listing:

using System.Windows;

using System.ComponentModel; using System.Threading;

namespace SimpleWorker

{

public partial class MainWindow : Window

{

BackgroundWorker bgWorker = new BackgroundWorker();

public MainWindow()

{

InitializeComponent();

// Set BackgroundWorker properties bgWorker.WorkerReportsProgress = true; bgWorker.WorkerSupportsCancellation = true;

// Connect handlers to BackgroundWorker object.

bgWorker.DoWork

+= DoWork_Handler;

bgWorker.ProgressChanged

+=

ProgressChanged_Handler;

bgWorker.RunWorkerCompleted +=

RunWorkerCompleted_Handler;

}

private void btnProcess_Click( object sender, RoutedEventArgs e )

{

if ( !bgWorker.IsBusy ) bgWorker.RunWorkerAsync();

}

private void ProgressChanged_Handler( object sender, ProgressChangedEventArgs args )

{

progressBar.Value = args.ProgressPercentage;

}

611

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

private void DoWork_Handler( object sender, DoWorkEventArgs args )

{

BackgroundWorker worker = sender as BackgroundWorker;

for ( int i = 1; i <= 10; i++ )

{

if ( worker.CancellationPending )

{

args.Cancel = true; break;

}

else

{

worker.ReportProgress( i * 10 ); Thread.Sleep( 500 );

}

}

}

private void RunWorkerCompleted_Handler( object sender, RunWorkerCompletedEventArgs args )

{

progressBar.Value =

0;

if ( args.Cancelled

)

MessageBox.Show(

"Process was cancelled.", "Process Cancelled" );

else

 

MessageBox.Show(

"Process completed normally.", "Process Completed" );

}

private void btnCancel_Click( object sender, RoutedEventArgs e )

{

bgWorker.CancelAsync();

}

}

}

612

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]