Delphi寫的DLL,OCX中如果使用了TThread.Synchronze(Proc),可能導致線程死鎖,原因是無法喚醒EXE中主線程,
Synchronze並不會進入EXE主線程消息隊列.
下面的程序自動解決此問題,只需要加入DLL,OCX工程文件中,在DLL,OCX中便可以使用TThread.Synchronze(Proc)了,無需再寫一行代碼。
//解決Delphi編譯的DLL,OCX文件中的線程調用 TThread.Synchronize後掛起無法再激活問題
//調用了TThread.Synchronize函數的所有工程請包含此文件
//僅需將此單元包含到工程文件即可
unit Lib.Common.DLLThread;
interface
implementation
uses Classes, Windows, Messages;
type
{ TDLLSystemController }
TDLLSystemController = class
private
FHandle: HWND;
FPrevWakeMainThread: TNotifyEvent;
procedure WakeMainThread(Sender: TObject);
procedure HookSynchronizeWakeup;
procedure UnhookSynchronizeWakeup;
protected
procedure WndProc(var Message: TMessage);
public
constructor Create;
destructor Destroy; override;
end;
var
FDLLController:TDLLSystemController;
{ TDLLSystemController }
constructor TDLLSystemController.Create;
begin
inherited;
if IsLibrary then
begin
FHandle := AllocateHWnd(WndProc);
HookSynchronizeWakeup;
end;
end;
destructor TDLLSystemController.Destroy;
begin
if IsLibrary then
begin
DeallocateHWnd(FHandle);
UnhookSynchronizeWakeup;
end;
inherited;
end;
procedure TDLLSystemController.WndProc(var Message: TMessage);
begin
case Message.Msg of
WM_NULL: CheckSynchronize;
else
Message.Result := DefWindowProc(FHandle, Message.Msg, Message.wParam, Message.lParam);
end;
end;
procedure TDLLSystemController.WakeMainThread(Sender: TObject);
begin
PostMessage(FHandle, WM_NULL, 0, 0);
end;
procedure TDLLSystemController.HookSynchronizeWakeup;
begin
FPrevWakeMainThread := Classes.WakeMainThread;
Classes.WakeMainThread := WakeMainThread;
end;
procedure TDLLSystemController.UnhookSynchronizeWakeup;
begin
Classes.WakeMainThread := FPrevWakeMainThread;
end;
initialization
if IsLibrary then FDLLController := TDLLSystemController.Create
else FDLLController:=nil;
finalization
if Assigned(FDLLController) then FDLLController.Free;
end.
我正好做了些這方面的工作,告訴我qq,一起聊聊
這個要看你這個函數是干什麼的,如果不涉及界面VCL或數據操作等需要同步的事情,那麼多個線程調用同一函數跟平常調用一個函數一樣,沒什麼特別的地方,反之,則需要注意每個線程之間的同步問題