[英]Which wait method to call in an infinite waiting thread for Delphi XE2
[英]delphi xe2 proper disposal of a pointer created from a thread which pointer is being sent to main thread
我想問幾個問題,讓我先向您解釋一下事情,您可以在本文下方看到問題。 我創建了一個多線程應用程序,該程序可以從數據庫讀取和更新數據。 線程使用sendmessage與主線程通信。 我將指針TRecord傳遞給sendmessage並將指針放置在主線程中。 以下是顯示流程結構的代碼段:
const WM_MY_MESSAGE = WM_USER + 0;
PTestPointerRecord : ^TTestPointerRecord;
TTestPointerRecord = record
i : integer;
end;
這是擴展的TThread類的execute事件。 除非線程被暫停或終止,否則它將連續運行。
procedure TSampleThreadClass.Execute;
var
TestPointerRecord : PTestPointerRecord;
FConnection : TConnectionObject;
FQuery : TQueryObject;
begin
while not Terminated do
begin
New(PTestPointerRecord);
FConnection := TConnectionObject.Create(nil);
FQuery := TQueryObject.Create(nil);
try
FConnection.connectionstring := 'path';
FConnection.loginPrompt := False;
FConnection.open;
FQuery.connection := FConnection;
FQuery.close;
FQuery.sql.clear;
FQuery.sql.add('select column1, column2 from table');
FQuery.open;
PTestPointerRecord.i := 0;
SendMessage(frmMain.handle, WM_MY_MESSAGE, 0, integer(PTestPointerRecord));
finally
FQuery.close;
FConnection.disconnect;
FreeAndNil(FQuery);
FreeAndNil(FConnection);
sleep(250);
end;
end;
end;
這是從線程接收消息的事件。
procedure TfrmMain.message(msg : TMessage);
var
TestPointerRecord : PTestPointerRecord;
begin
TestPointerRecord := PTestPointerRecord(msg.lParam);
try
edit1.Text := inttostr(TestPointerRecord.i);
finally
Dispose(TestPointerRecord);
end;
end;
該應用程序將用作服務類型的應用程序,該應用程序將一直持續運行。
問題:
1.我是否正確放置了指針?
2.當我在應用程序運行時檢查任務管理器時,發現在“進程”選項卡下,我注意到Memory(私有工作集)不斷增加。 這樣好嗎
關於所有
我嘗試過建議David Heffernan使用單獨的句柄而不是使用主窗體的句柄。 這個建議並沒有真正解決問題,但多虧了David,他值得一用,因為他對使用主窗體的句柄接收消息並且重新繪制或重新創建窗口時可能出現的問題做了重點說明。
通過更深入地探索我的代碼,進行調試,反復試驗。 我發現在創建連接並查詢數據庫時問題再次發生。 注意,我正在使用ZeosLib連接到數據庫,似乎每次線程循環執行數據庫操作時,工作的專用內存都在不斷增加,這是我不確定Zeoslib是否完全線程安全的。 所以我改用ADO,一切順利。 工作專用內存保持穩定。
感謝您的所有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.