我正在开发一个多线程应用程序(RAD Studio XE5).在应用程序的开始,我创建一个单独的线程,只要主表单生效.
我能够将消息从线程发送到我的应用程序中创建的任何表单,但是我找不到相反的方法,从主VCL线程向工作线程发送消息.
在创建主窗体时,我创建了工作线程并将句柄复制到公共变量中:
serverThread := TMyThread.Create(True, ServerPort + 1);
serverThreadHandle := serverThread.Handle; // SAVE HANDLE
serverThread.Start;
Run Code Online (Sandbox Code Playgroud)
然后(从另一种形式FrmSender)我向线程发送一条消息:
PostMessage(uMain.serverThreadHandle, UM_LOC_VCLMSG, UM_LOC_VCLMSG, Integer(PStrListVar));
Run Code Online (Sandbox Code Playgroud)
这是线程的执行过程:
procedure TMyThread.Execute;
var
(..)
vclMSG : TMsg;
str1, str2 : string;
(..)
begin
while not(Terminated) do
begin
Sleep(10);
if Assigned(FrmSender) then
if FrmSender.HandleAllocated then
if PeekMessage(vclMSG, FrmSender.Handle, 0, 0, PM_NOREMOVE) then
begin
if vclMSG.message = UM_LOC_VCLMSG then
begin
try
pStrListVar := pStrList(vclMSG.lParam);
str1 := pStrListVar^.Strings[0];
str2 := pStrListVar^.Strings[1];
finally
Dispose(pStrListVar);
end;
end;
end;
(.. do other …Run Code Online (Sandbox Code Playgroud) 我在理解指针的行为方面遇到了一些麻烦.我有一个非常简单的例子来证明:
type
PSL = ^TStringList;
...
var
myPSL : PSL;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
New(myPSL);
myPSL^ := TStringList.Create;
myPSL^.Add('String 1');
myPSL^.Add('String 2');
myPSL^.Add('String 3');
end;
...
procedure TForm1.FormDestroy(Sender: TObject);
begin
Dispose(myPSL);
end;
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我得到了这个内存泄漏报告
29 - 36 bytes: UnicodeString x 3
37 - 44 bytes: Unknown x 1
85 - 92 bytes: TStringList x 1
Run Code Online (Sandbox Code Playgroud)
现在,如果我打电话
myPSL^.Free;
Run Code Online (Sandbox Code Playgroud)
在处理指针之前,没有报告任何内容.
我不明白为什么会这样.我知道调用New()分配足够的内存(基于指针的类型)并调用Dispose()负责释放相同的内存,那么为什么我需要释放指针,好像它是一个"真正的"对象?
谢谢!
我正在使用自定义Windows消息将工作线程中的信息交换到主VCL线程中的表单.每当我需要通过消息发送一些数据时,我这样做:
type
PntStr = ^string;
Run Code Online (Sandbox Code Playgroud)
然后PostMessage()
var
pointString : PntStr;
(...)
New(pointString);
pointString^ := mystring;
PostMessage(frmDest.Handle, UM_THREADMSG, UM_MYEVENT1, LPARAM(pointString));
Run Code Online (Sandbox Code Playgroud)
在接收表格上
try
myStrP := PntStr(MSG.LParam);
myfunction(myStrP^);
finally
Dispose(myStrP);
end;
Run Code Online (Sandbox Code Playgroud)
这是处理指针分配的内存的正确方法吗?在指针上调用Dispose()是否可以释放内存?
我很难从基本的TThread类实现多层继承.
基于我对OOP的了解应该是可能的,但也许它不能应用于线程.
我的目标是使用TMyBaseThread来实现后代类通用的所有代码.这是我尝试过的:
TMyBaseThread = class(TThread)
private
procedure BuildBaseObjects(aParam : TParam);
procedure Execute; virtual; abstract;
protected
constructor Create(Param : TParam); reintroduce; virtual;
end;
TMyFileThread = class(TMyBaseThread)
private
procedure Execute; reintroduce;
public
constructor Create(OtherParam : TOtherParam); reintroduce; overload;
end;
TMyDBThread = class(TMyBaseThread)
private
procedure Execute; reintroduce;
public
constructor Create(aDB : TDatabase); reintroduce; overload;
end;
implementation
constructor TMyBaseThread.Create(Param : TParam);
begin
inherited Create(False);
Self.BuildBaseObjects(Param);
[do some stuff]
end;
constructor TMyFileThread.Create(OtherParam : TOtherParam);
var
param : TParam;
begin
inherited Create(param);
[do some other …Run Code Online (Sandbox Code Playgroud)