Http://www.cnblogs.com/railgunman/archive/2010/12/08/1900688.html
The Bmdthread control is a fairly mature line program that allows you to quickly create and manage threads.
You can download the Bmdthread control to a csdn or a box.
Below we use the multi-threaded simulation client sends the file example to understand it simply.
Place a tidclient,tbmdthread,tbmdthreadgroup in the form. Three Tedit, two buttons (start thread, end thread), one memo to accept thread result information
Function: Use idtcpclient to send files to the specified server, dynamically create the number of threads synchronously send files.
Start creating our threading unit.
Create a new unit, save as Threadunit.pas.
You need to reference the Bmdthread cell in the Unit interface section. To facilitate the following code writing, the windows,classes unit is also referenced.
First, because IP, port, the number of threads that need to be created is dynamic, so it needs to be provided to our thread.
Note: Tfilestream.create last parameter meaning:
Open mode:
Fmcreate: Creates a file with the specified file name and opens it if the file already exists.
Fmopenread: Open the specified file as read-only
Fmopenwrite: Open the specified file as a write-only method
Fmopenreadwrite: Open the specified file as a write
Sharing mode:
Fmsharecompat: Shared mode compatible with FCBs
Fmshareexclusive: Do not allow other programs to open the file in any way
Fmsharedenywrite: Does not allow other programs to open the file in a write manner
Fmsharedenyread: Do not allow other programs to open the file in read mode
Fmsharedenynone: Other programs can open the file in any way
The code is as follows:
Unit threadunit;
Interface
Uses
Windows, Classes, Sysutils, Bmdthread, idtcpclient;
Type
Tsendmsg = procedure (owner:tobject; Threadid:integer; msg:string) of object;
Tsendthread= Class (Tbmdthread)
Private
fhost:string;
Fport:integer;
Fthreadid:integer;
fsendmsg:string;
fonsendmsg:tsendmsg;
Procedure Dosend;
Procedure setsendmsg (const VALUE:TSENDMSG);
Protected
Procedure Execute (); Override
Procedure dosendmsg (Sender:tobject);
Public
Constructor Create (owner:tcomponent; host:string; Port, Threadid:integer);
destructor Destroy (); Override
Property onsendmsg:tsendmsg read Fonsendmsg write setsendmsg;
End
Implementation
{Tsendthread}
Constructor Tsendthread.create (owner:tcomponent; host:string; Port, Threadid:integer);
Begin
Fhost:= Host;
Fport:= Port;
Fthreadid:= ThreadID;
Inherited Create (Owner);
End
destructor Tsendthread.destroy;
Begin
inherited;
End
Procedure Tsendthread.dosend;
Var
Idtcpclient:tidtcpclient;
Fs:tfilestream;
filename:string;
Begin
inherited;
Try
filename:= ' E:\text.txt ';
Try
Idtcpclient: = Tidtcpclient.create (nil);
Try
Idtcpclient.host: = Fhost;
Idtcpclient.port: = FPort;
Idtcpclient.connect;
fs:= tfilestream.create (FileName, fmopenread or Fmsharedenynone);
Fs. position:= 0;
Fs. Seek (0, 0);
Idtcpclient.writeln (' <SEND> ' + FileName);
Idtcpclient.writestream (FS);
Fsendmsg: = ' send success ';
Thread.synchronize (DOSENDMSG);
except on E:exception do
Begin
Fsendmsg: = ' connection error: ' + e.message;
Thread.synchronize (DOSENDMSG);
End
End
Finally
Freeandnil (FS);
Idtcpclient.disconnect;
Idtcpclient.free;
End
Except
End
End
Procedure tsendthread.dosendmsg (Sender:tobject);
Begin
If Assigned (fonsendmsg) Then
Fonsendmsg (Sender, Fthreadid, fsendmsg);
End
Procedure Tsendthread.execute;
Begin
While not thread.terminated do//If you want your code to go on until the thread ends, you can do so
Dosend;
End
Procedure tsendthread.setsendmsg (const VALUE:TSENDMSG);
Begin
Fonsendmsg:= Value;
End
End.
Main Unit Code:
Unit mainunit;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Idbasecomponent, Idcomponent, Idtcpconnection,
Idtcpclient, Bmdthread;
Type
TForm1 = Class (Tform)
Bmdthread1:tbmdthread;
Bmdthreadgroup1:tbmdthreadgroup;
Idtcpclient1:tidtcpclient;
Edt_host:tedit;
Lbl1:tlabel;
Lbl2:tlabel;
Edt_port:tedit;
Lbl3:tlabel;
Edt_count:tedit;
Btn_send:tbutton;
Btn_stop:tbutton;
Mmo1:tmemo;
Btn1:tbutton;
Procedure Btn_sendclick (Sender:tobject);
Procedure getmsg (Sender:tobject; Threadid:integer; msg:string);
Procedure Btn_stopclick (Sender:tobject);
Procedure Btn1click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
Uses threadunit;
{$R *.DFM}
Procedure Tform1.btn_sendclick (Sender:tobject);
Var
I:integer;
Sendthread:tsendthread;
Begin
Btn_stop.click;
For i:= 1 to Strtoint (Edt_count.text) do
Begin
sendthread:= tsendthread.create (Self,edt_host.text, Strtoint (Edt_port.text), I);
Try
Sendthread.threadgroup:= BMDThreadGroup1;
Sendthread.onsendmsg:= getmsg;
Except
Sendthread.free;
End
End
For i:= 0 to Bmdthreadgroup1.threadscount-1 do
Begin
Sendthread:= Tsendthread (Bmdthreadgroup1.threaditems[i]);
Try
Sendthread.start;
Except
On E:exception do
Begin
Sendthread.stop;
SendThread.Thread.WaitFor;
End
End
End
End
Procedure tform1.getmsg (sender:tobject; Threadid:integer; msg:string);
begin
Mmo1. Lines.add (FormatDateTime (' Yyyy-mm-dd hh:mm:ss ', now) + ' thread ID: ' + inttostr (ThreadID) + MSG);
End;
Procedure Tform1.btn_stopclick (Sender:tobject);
Var
Sendthread:tbmdthread;
Begin
Bmdthreadgroup1.stop ();
While Bmdthreadgroup1.threadscount > 0 do
Begin
Sendthread:= Bmdthreadgroup1.threads[bmdthreadgroup1.threadscount-1];
Try
If Sendthread.thread <> Nil Then
Begin
Sendthread.stop ();
SendThread.Thread.WaitFor;
End
Except
End
Bmdthreadgroup1.removethread (Sendthread);
End
End
Procedure Tform1.btn1click (Sender:tobject);
Begin
Mmo1. Clear;
End
End.
Source: http://www.wesoho.com/article/Delphi/2882.htm
Bmdthread controls dynamically creating multithreaded examples