Threading in Delphi for .NET - Delegates in Delphi
(Page 4 of 15 )
Regardless of the method used for manually created threads, both make use of delegates. A delegate is a .NET term for an object-oriented callback method that is type-safe. Similar to event handlers in Delphi, delegates provide a mechanism for multiple objects to be notified when called. Listings 14.2 and 14.3 demonstrate how delegates are used in Delphi when creating a thread.
Listing 14.2 Creating Threads Using Instance Methods
1: program instancethreads;
2: {$APPTYPE CONSOLE}
3:
4: //
5: // This example demonstrates how to use native .NET methods to create a
6: // thread on a class with an instance method.
7: //
8:
9: uses
10: System.Threading;
11:
12: type
13: D4DNInstanceThread = class
14: private
15: FStartNumber : integer;
16: public
17: // ThreadMePlease will be executed on a different thread
18: procedure ThreadMePlease;
19: property StartNumber : integer write FStartNumber;
20: end;
21:
22: procedure D4DNInstanceThread.ThreadMePlease;
23: var
24: stop : integer;
25: curNum : integer;
26: begin
27: curNum := FStartNumber;
28: stop := FStartNumber + 10;
29: while curNum < stop do
30: begin
31: writeln('Thread ', System.Threading.Thread.CurrentThread.Name ,
32: ' current value is ',curNum);
33: inc(curNum);
34: Thread.Sleep(3);
35: end;
36: end;
37:
38: var
39: ThreadWork1 : D4DNInstanceThread;
40: ThreadWork2 : D4DNInstanceThread;
41: Thread1 : Thread;
42: Thread2 : Thread;
43: begin
44: writeln('Starting threading instance method example...');
45:
46: // Create D4DNInstanceThread instance
47: ThreadWork1 := D4DNInstanceThread.Create;
48: ThreadWork1.StartNumber := 10;
49:
50: // Create the thread, specifying the instance method to execute
51: Thread1 := Thread.Create(@ThreadWork1.ThreadMePlease);
52: Thread1.Name := 'one';
53:
54: // Create another instance of D4DNInstanceThread
55: ThreadWork2 := D4DNInstanceThread.Create;
56: ThreadWork2.StartNumber := 100;
57:
58: // Create the second thread, specifying the instance method to execute
59: Thread2 := Thread.Create(@ThreadWork2.ThreadMePlease);
60: Thread2.Name := 'two';
61:
62: // Finally start the threads
63: Thread1.Start;
64: Thread2.Start;
65:
66: // Wait for the two threads to finish
67: Thread1.Join;
68: Thread2.Join;
69:
70: // Wait for the user to see the results
71: writeln('Done');
72: readln;
73: end.
Note: Find the code on the CD: \Code\Chapter 14\Ex01\.
Listing 14.2 demonstrates how to create a thread using an instance method. Any instance method that does not have any parameters can be executed on a thread. Look at the Thread.Create() constructor call (shown in Listing 14.2 on line 51). The parameter to the Thread.Create constructor is the address of the ThreadMePlease method. Under the covers, the Delphi compiler is creating a ThreadStart delegate. Other languages, such as C#, require a few more lines of code to accomplish the same task.
Note - Although the ThreadStart delegate does not allow for passing parameters, using the instance method provides the opportunity to use either the constructor or properties to pass additional information needed by the thread. Listing 14.2 demonstrates this by setting the StartNumber.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Creating Threads Using Static Methods >>
More .NET Articles
More By Xavier Pacheco