Delphi 2010 threads [closed]
Can someone post the simplest thread example for Delphi 2010 that for example puts some text into a memo when button clicked? With implementation and all. Thanks.
Update: Ok, just a simple thread that does something. No need for a memo. Thanks.
There's a nice thread demo, including GUI/Synchronize, that ships with Delphi. In D2005, it's in: C:\Program Files\Borland\BDS\3.0\Demos\DelphiWin32\VCLWin32\Threads
Simplest example I can imagine: (Suppose you have Memo1 on Form1)
procedure TForm1.Button1Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(procedure
var
slThread: TStringList;
i: Integer;
begin
//do something in thread
slThread := TStringList.Create;
try
for i := 0 to 100 - 1 do
begin
slThread.Add(Format('Value %D',[i]));
end;
//update gui
TThread.Synchronize(nil, procedure
begin
Form1.Memo1.Lines.AddStrings(slThread);
end);
finally
slThread.Free;
end;
end).Start;
end;
Although I won't recommend using it, because it has some disadvantages. It is better to descend your own TThread class but for your question this example fits nicely.
As it has been pointed out, creating a thread to update the GUI isn't a good design. It would be better to have your threads do some actual work, and allow the main thread update your display.
While Delphi also offers the TThread class to make creating/managing threads easier, you may also want to have a look at using Delphi's BeginThread
function for executing simple threads. There is an example on the Delphi Basics website, which I have reproduced here:
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
interface
uses
Forms, Dialogs, Windows, SysUtils;
type
TMsgRecord = record
thread : Integer;
msg : string[30];
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
Implementation
{$R *.dfm} // Include form definitions
ThreadVar // We must allow each thread its own instances
// of the passed record variable
msgPtr : ^TMsgRecord;
// Private thread procedure to show a string
function ShowMsg(Parameter : Pointer) : Integer;
begin
// Set up a 0 return value
Result := 0;
// Map the pointer to the passed data
// Note that each thread has a separate copy of msgPtr
msgPtr := Parameter;
// Display this message
ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg,
200*msgPtr.thread, 100);
// End the thread
EndThread(0);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
id1, id2 : LongWord;
thread1, thread2 : Integer;
msg1, msg2 : TMsgRecord;
begin
// set up our display messages
msg1.thread := 1;
msg1.msg := 'Hello World';
msg2.thread := 2;
msg2.msg := 'Goodbye World';
// Start the first thread running asking for users first name
thread1 := BeginThread(nil,
0,
Addr(ShowMsg),
Addr(msg1),
0,
id1);
// And also ask for the surname
thread2 := BeginThread(nil,
0,
Addr(ShowMsg),
Addr(msg2),
0,
id2);
// Ensure that the threads are only closed when all done
ShowMessagePos('Press this when other dialogs finished.', 200, 300);
// Finally, tidy up by closing the threads
CloseHandle(thread1);
CloseHandle(thread2);
end;
end.
Here is also another example of how to use threads with parameters (using BeginThread).
The best source of general information about threading in Delphi, bar none, can be found here - http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/ToC.html. Have a look here before going anywhere else.
精彩评论