如何在程序代码中自行建立ThreadObject?(50分)

N

nicol

Unregistered / Unconfirmed
GUEST, unregistred user!
通常建立ThreadObject是通过New/ThreadObject建立的。不用这种方式,如何在程序代码中自行建立ThreadObject?
 
看demo/threads
 
手工写类,继承之TThread,写入自己的Excute;

用Win API中的CreateThread:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
DWORD dwStackSize, // initial thread stack size, in bytes
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to returned thread identifier
);
 
@_@是什么意思?高度近视?还是...??:)
 
瞪大我的双眼皮:)
 
直接输入:并把自己想添加的东西放进去。
type
TMyThread=class(TThread)
private
procedure Execute;override;
end;
 
关注!
在何时释放它?
 
释放:
在constructor TYourThread.Create(...);
begin
...
FreeOnTerminate := true;
...
end;
 
用API太麻烦了,
还要考虑线程同步之类的问题
还是直接继承TThread算了
 
在TYPE段里写:
TYourThread=class(TThread)
private
procedure Execute;override;
end;

在VAR里声明对象实例,如:
YourThread:TYourThread;
在使用线程前写:
YourThread:=TYourThread.Create(false);
//立即执行
 
New/ThreadObject和动态创建没太大区别
好好看看YourThread.pas,然后照搬过去就可以了.
 
多人接受答案了。
 
顶部