我想一个极为简单多线程的例子:
主线程在文本况中输入0-9中的一个数(1位),
另一个线程便复制同一个数显示在另一个文本框内,
给个提示,或是源码
主线程在文本况中输入0-9中的一个数(1位),
另一个线程便复制同一个数显示在另一个文本框内,
给个提示,或是源码
解决方案 »
- 关于数据批量修改提交,采用DBGrid 的几一些问题.
- 如何获得指定文件夹下 文件的名称列表(包括文件名及及后缀名)
- delphi 如何调用c#写的dll?能不能给个demo阿?我找了好多地方,都没有找到阿!谢谢各位了!
- 关于正玄函数的小问题(很简单的)在线等
- 征求选择技术的两个问题!
- Remote Desktop 管理不了我的远程服务器了!!急,急,急!
- 如何获取当前系统的屏幕颜色模式: 是256 色,增强色(16位)还是真彩色(32位)??急急!!
- sql 问题。。
- 用delphi调试器调试的时候,只要一重置delphi就死呢?有什么办法吗?不过用的是盗版的!
- 在调试控件的时候,我怎样把控件放到窗体上面去?
- 相学汇编
- 有没有Delphi做的选修课管理系统啊 跪求
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TNumThread = class(TTHread)
private
FNum: Integer;
procedure ShowData;
protected
procedure Execute; override;
public
ThreadNo: Integer;
end;var
lineCount: Integer;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TNumThread.ShowData;
begin
Form1.Memo1.lines.Add(IntToStr(FNum) + ' ' + IntToStr(ThreadNo));
end;
procedure TNumThread.Execute;
begin
inherited;
FreeOnTerminate := True;
while lineCount < 10000 do
begin
InterlockedIncrement(lineCount);
FNum := lineCount;
// 同步
Synchronize(ShowData);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
numThread:array [0..4] of TNumThread ;
I: Integer;
begin
memo1.Clear ;
lineCount := 0;
for I := 0 to 4 do
begin
numThread[I] := TNumThread.Create(true);
numThread[I].ThreadNo := I;
numThread[I].Resume ;
end;
end;end.参考一下这个
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, myThread, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
ListBox1: TListBox;
Label1: TLabel;
StaticText1: TStaticText;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure OnThreadTerminate(Sender: TObject);
end;var
Form1: TForm1;
th: array[0..4] of TThreadRandom;
CS: TRTLCriticalSection;
DoneFlags: Integer=0;
implementation
{$R *.dfm}{ TForm1 }procedure TForm1.OnThreadTerminate(Sender: TObject);
begin
inc(DoneFlags);
listbox1.Items.Add((sender as TThreadRandom).ThreadName);
if DoneFlags=5 then DeleteCriticalSection(CS);
end;procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
InitializeCriticalSection(CS);
listbox1.Clear;
memo1.Clear;
for i:=0 to 4 do
th[i]:=TThreadRandom.Create('Thread# ' + IntToStr(i));
end;end.
unit myThread;interfaceuses
Classes, SysUtils, Windows;type
TThreadRandom = class(TThread)
private
{ Private declarations }
FThreadName: String;
procedure AddToMemo;
procedure AddToList;
procedure GetLines;
protected
procedure Execute; override;
public
property ThreadName: String Read FThreadName;
constructor Create(threadName: String);overload;
destructor Destroy; override;
end;var
FLines: Integer=0;implementation
uses
main;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TThreadRandom.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TThreadRandom }constructor TThreadRandom.Create(threadName: String);
begin
FreeOnTerminate:=true;
OnTerminate:= form1.OnThreadTerminate;
FThreadName:=threadName;
inherited Create(false);
end;procedure TThreadRandom.Execute;
begin Synchronize(GetLines);
while(FLines<10000) do
begin
windows.EnterCriticalSection(CS);
Synchronize(AddToMemo);
Synchronize(GetLines);
Windows.LeaveCriticalSection(CS);
end;end;destructor TThreadRandom.Destroy;
begin
inherited Destroy;
end;procedure TThreadRandom.AddToMemo;
begin
Randomize;
form1.Memo1.Lines.Add(FThreadName + ' 添加:'+ IntToStr(Random(10000)));
end;procedure TThreadRandom.AddToList;
begin
form1.ListBox1.Items.Add(FThreadName+' 已终止。');
end;procedure TThreadRandom.GetLines;
begin
FLines:=form1.Memo1.Lines.Count;
form1.StaticText1.Caption:=inttostr(FLines);
end;end.program ThreadSyn;uses
Forms,
main in 'main.pas' {Form1},
myThread in 'myThread.pas';{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
这两个例子参考一下