unit test;uses 'your unit',classes; type Ttestthread=class(tthread) protected procedure execute;override; end;procedure Ttestthread.execute; begin adotable2.active:=true end; your unit add uses test;test1:Ttestthread; test1:=testthread.create(true); test1.resume;
your unit add uses test;test1:Ttestthread; test1:=testthread.create(true); test1.resume; 这段加哪里头.
不谢 对阿,不过多个线程间访问全局变量是要注意同步问题 具体我也不太懂,呵呵 FreeOnTerminate := True是线程运行完毕把自己释放掉,看看下面得源代码就可知道 function ThreadProc(Thread: TThread): Integer; var FreeThread: Boolean; begin {$IFDEF LINUX} if Thread.FSuspended then sem_wait(Thread.FCreateSuspendedSem); {$ENDIF} try if not Thread.Terminated then try Thread.Execute; except Thread.FFatalException := AcquireExceptionObject; end; finally FreeThread := Thread.FFreeOnTerminate; Result := Thread.FReturnValue; Thread.FFinished := True; Thread.DoTerminate; if FreeThread then Thread.Free; //这里就可知道把自己释放掉 {$IFDEF MSWINDOWS} EndThread(Result); {$ENDIF} {$IFDEF LINUX} // Directly call pthread_exit since EndThread will detach the thread causing // the pthread_join in TThread.WaitFor to fail. Also, make sure the EndThreadProc // is called just like EndThread would do. EndThreadProc should not return // and call pthread_exit itself. if Assigned(EndThreadProc) then EndThreadProc(Result); pthread_exit(Pointer(Result)); {$ENDIF} end; end;而ThreadProc是建立线程时的线程函数 在TThread.Create里有这么一句 FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, FThreadID);
type
Ttestthread=class(tthread)
protected
procedure execute;override;
end;procedure Ttestthread.execute;
begin
adotable2.active:=true
end;
your unit add
uses test;test1:Ttestthread;
test1:=testthread.create(true);
test1.resume;
uses test;test1:Ttestthread;
test1:=testthread.create(true);
test1.resume;
这段加哪里头.
在某个函数中如按钮事件加入
test1:=testthread.create(true);
test1.resume;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Ttestthread=class(tthread)
protected
procedure execute;override;
end;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
uses test;
test1:Ttestthread;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
test1:=testthread.create(true);
test1.resume;end;
procedure Ttestthread.execute;
begin
adotable2.active:=true
end;
end.我全加上去了,运行还是不行.
线程是一个类呀,老兄
procedure Ttestthread.execute;//线程里的东西,怎么跑着了
begin
adotable2.active:=true
end;
建立线程单元
File->New->Thread Object
OK?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids;type
TMyThread = class(TThread)
protected
procedure Execute; override;
end; TForm1 = class(TForm)
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FMyThread: TMyThread; public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FMyThread := TMyThread.Create(True);
FMyThread.FreeOnTerminate := True;
FMyThread.Resume;
end;{ TMyThread }procedure TMyThread.Execute;
begin
Form1.Table1.Active := True;
end;end.
我运行了通过了.
有三百多分, 不过不在同一张贴子上, 这些问题都没有人回答,放那好久, 请进来领分.
谢谢谢谢两位大哥.我想问一下
FMyThread.FreeOnTerminate := True;
是起什么作用.http://www.csdn.net/expert/topic/387/387694.shtm;
http://www.csdn.net/expert/topic/379/379266.shtm;
对阿,不过多个线程间访问全局变量是要注意同步问题
具体我也不太懂,呵呵
FreeOnTerminate := True是线程运行完毕把自己释放掉,看看下面得源代码就可知道
function ThreadProc(Thread: TThread): Integer;
var
FreeThread: Boolean;
begin
{$IFDEF LINUX}
if Thread.FSuspended then sem_wait(Thread.FCreateSuspendedSem);
{$ENDIF}
try
if not Thread.Terminated then
try
Thread.Execute;
except
Thread.FFatalException := AcquireExceptionObject;
end;
finally
FreeThread := Thread.FFreeOnTerminate;
Result := Thread.FReturnValue;
Thread.FFinished := True;
Thread.DoTerminate;
if FreeThread then Thread.Free; //这里就可知道把自己释放掉
{$IFDEF MSWINDOWS}
EndThread(Result);
{$ENDIF}
{$IFDEF LINUX}
// Directly call pthread_exit since EndThread will detach the thread causing
// the pthread_join in TThread.WaitFor to fail. Also, make sure the EndThreadProc
// is called just like EndThread would do. EndThreadProc should not return
// and call pthread_exit itself.
if Assigned(EndThreadProc) then
EndThreadProc(Result);
pthread_exit(Pointer(Result));
{$ENDIF}
end;
end;而ThreadProc是建立线程时的线程函数
在TThread.Create里有这么一句
FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, FThreadID);
http://www.csdn.net/expert/topic/387/387694.shtm;
http://www.csdn.net/expert/topic/379/379266.shtm;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids;type
TMyThread = class(TThread)
protected
procedure Execute; override;
end; TForm1 = class(TForm)
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FMyThread: TMyThread; public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FMyThread := TMyThread.Create(True);
FMyThread.FreeOnTerminate := True;
FMyThread.Resume;
end;{ TMyThread }procedure TMyThread.Execute;
begin
Form1.Table1.Active := True;
end;end. 我还想启动一个Table2.active:=True;怎么做,也是线程(加上两百分)