Delphi7 Send a message from the subclinger to the main thread trigger event execution

xiaoxiao2021-03-06  81

In the operation of the database, sometimes use a sub-thread to make the background data operation. For example, data backup, what is transferred. Other operations can also be performed in the main window. Sometimes a data file is handled in the background, you want to send a message to the main window, so that the main window real-time display processing progress is on the window (visual), while log processing, etc. I am using the following method: [1] API function: registerWindowsMessage -------------------- function: This function defines a new window Message, this message ensures that it is unique in the system. The returned message value can be used when calling a function sendMessage or PostMessage. Function RegisterWindowMessage (LPSTRING: PCHAR): uint; stdcall; sendNotifyMessage --------------------- Function function: This function sends the specified message to a window. If the window is created by the calling thread; this function calls the window program for the window and waits for the window program to return to the message. If the window is created by a different thread, this function passes the message to the window program and returns immediately, and the message is not waiting to process the message. SendnotifyMessage (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM iParam); BroadcastsystemMessage -------------------- Function function: This function sends a message to the specified recipient . The recipient can be an application, install the drive, network drive, system-level device driver, or a combination of these system components. [2] Process: TYPE TFORM1 = Class (TFORM) ................................ private msg: cardinal; protected procedure WndProc (VAR message: tMessage); OVERRIDE; public ............................. End; var form1: tform1; msgstrlist: tstringlist Msgstrlock: tcriticalsection;

ImplementationUses threadcommunication_unit; {$ r * .dfm}

Procedure TForm1.FormCreate (Sender: TOBJECT); Begin Msg: = RegisterWindowMessage ('wm_threadmsg'); msgstrlist: = tstringlist.create;

procedure TForm1.WndProc (var Message: TMessage); begin if Message.Msg = Msg then begin MsgStrLock.Enter; if MsgStrList.Count> 0 then begin Caption: = MsgStrList.Strings [0]; MsgStrList.Delete (0); end Msgstrlock.Leave; showMessage ('received message' INTOSTR (Message.msg)); end; end;

Procedure tform1.button1click (sender: Tobject); Begin TthreadCommunication.create (msg, memo1); end; ........................... ..initialization msgstrlock: = TcriticalSection.create; Finalization Msgstrlock.free; End. A subclinger unit: Unit threadcommunication_unit; InterfaceUses Classes, stdctrls;

type TThreadCommunicaiton = class (TThread) private FMsg: Cardinal; FMemo: TMemo; protected procedure Execute; override; procedure SendMsg; public constructor Create (aMsg: Cardinal; am: TMemo); virtual; end;

ImplementationUses Messages, Windows, Dialogs, Sysutils, Threadmsg; {Tthreadcommunicaiton}

Crsinical TTHREADCOMMUNICAITON.CREATE (AM: TMEMO); begin inherited create (true); fmsg: = amsg; fmemo: = am; freeonterminate: = true; resume;

Procedure tthreadcommunicaiton.execute; begin synchronize; end;

转载请注明原文地址:https://www.9cbs.com/read-94516.html

New Post(0)