Lab Practice with Delegates and Threads

In this lab, you will create an application similar to the application you created in Lesson 1. Your application will execute a time-consuming process on a separate thread that can be cancelled, reports progress, and notifies the user when finished. First, you will use delegates and asynchronous invocation to implement this functionality. After that is complete, you will modify your application to use threads.

► Exercise 1: Practice with Delegates

1. Open the partial solution for the first lab from its location on the companion CD. You will find it in the Code folder in the Chapter 13 subfolder.

2. In the Designer, right-click the form and choose View Code. Add the following method to the application:

Private Sub TimeConsumingMethod(ByVal seconds As Integer) For j As Integer = 1 To seconds

System.Threading.Thread.Sleep(1000) Next End Sub

private void TimeConsumingMethod(int seconds) {

System.Threading.Thread.Sleep(1000);

3. Add a delegate to the application that is appropriate for TimeConsuming-Method, as shown below:

' VB Private Delegate Sub TimeConsumingMethodDelegate(ByVal j As Integer) // C# private delegate void TimeConsumingMethodDelegate(int seconds);

4. Add a method to report progress that sets the value of the ProgressBar control in a thread-safe manner and a delegate to that method, as shown below:

Public Delegate Sub SetProgressDelegate(ByVal val As Integer) Public Sub SetProgress(ByVal val As Integer) If ProgressBar1.InvokeRequired = True Then

Dim del As New SetProgressDelegate(AddressOf SetProgress) Me.Invoke(del, New Object() {val}) Else

ProgressBar1.Value = val End If End Sub

public delegate void SetProgressDelegate(int val);

public void SetProgress(int val) {

if (ProgressBar1.InvokeRequired) {

SetProgressDelegate del = new SetProgressDelegate(SetProgress); this.Invoke(del, new object[]{val});

ProgressBar1.Value = val;

5. Add the following line of code to the For loop of TimeConsumingMethod to report the progress of the method:

6. Add a Boolean variable named Cancel to the application as shown below:

Private Cancel As Boolean // C#

bool Cancel;

7. Add the following lines of code to the For loop of TimeConsumingMethod: ' VB

if (Cancel) break;

8. Add the following code after the For loop of TimeConsumingMethod:

If Cancel Then

MsgBoxC'Cancelled") Cancel = False Else

MsgBox("Comp1eted") End If

System.Windows.Forms. MessageBox.ShowC'Cancelled"); Cancel = false;

System.Windows.Forms.MessageBox.Show("Comp1ete");

9. In the Designer, double-click the GO! button to open the default event handler for the button's Click method and add the following code:

Dim del As New TimeConsumingMethodDe1egate(AddressOf TimeConsumingMethod) de1.BeginInvoke(CInt(TextBox1.Text), Nothing, Nothing)

TimeConsumingMethodDelegate del = new

TimeConsumingMethodDelegate(TimeConsumingMethod); del.BeginInvoke(int.Parse(TextBox1.Text), null, null);

10. In the Designer, double-click the Cancel button to open the default event handler for the button's Click method and add the following code:

Cancel = true;

11. Press F5 to compile and test your application.

► Exercise 2: Using Threads

In this exercise you will modify the application created in the first exercise to use a Thread object instead of a delegate.

1. Open your completed application from Exercise 1 or the completed version on the companion CD in the Code folder in the Chapter 13 subfolder.

2. In the Code window, change the signature of TimeConsumingMethod as follows:

Private Sub TimeConsumingMethod(ByVal time As Object) // C#

private void TimeConsumingMethod(object Time)

3. Add the following lines of code to the first lines of TimeConsumingMethod.

Dim seconds As Integer seconds = CInt(Time)

int seconds; seconds = (int)Time;

4. In the Designer, double-click the GO! button and replace the code that is there with the following code:

Dim aThread As New Threading.Thread(AddressOf TimeConsumingMethod) aThread.Start(CInt(TextBox1.Text))

System.Threading.Thread aThread = new

System.Threading.Thread(TimeConsumingMethod); aThread.Start(int.Parse(textBox1.Text));

5. Press F5 to compile and test your application.

Was this article helpful?

0 0

Post a comment