Sending messages between Delphi threads

Source: Internet
Author: User

Thread B sends a message to thread A, SendMessage (thread A handle, MSG, wparam parameter, lparam parameter);

Thread A overrides WndProc (Var message:tmessage);

1 {thread B sends a message}2 UnitThreadunit;3 4 Interface5 6 uses7 Classes, ActiveX, Businessservice, Dialogs, Constant, Windows, Messages, sysutils;8 9 typeTenTupgradeappthread =class(TThread) One   protected A     procedureExecuteOverride; -   Private -Appfiledir:string; the Manualcheck:boolean; -    Public -Mainfromhandle:hwnd;//main thread handle -     Constructor Create(Appfiledir:string; manualcheck:boolean); +   End; -  + Const {0x0400-0x7fff} AWM_UPDATE_UI1 = Wm_user + $1001; atWm_update_ui2 = Wm_user + $1002; -  - var - Upgradeappthread:tupgradeappthread; -  - Implementation in  - ConstructorTupgradeappthread.Create(Appfiledir:string; manualcheck:boolean); to begin +   inherited Create(False); -Self.appfiledir: =Appfiledir; theSelf.manualcheck: =Manualcheck; * End; $ Panax Notoginseng procedureTupgradeappthread.execute; - var theRes:string; + sdata:tcopydatastruct; A begin theFreeonterminate: =True; +   Try -Res: =upgradeapplication (Appfiledir, False); $     ifres ='0'  Then $     begin -       //The full version of the customer update is not detected for DLL update detection -Res: =Upgradedllonline (appfiledir); the     End; - Wuyi     ifres ='0'  Then the     begin -       ifSelf.manualcheck Then Wu       begin -ShowMessage ('Booz Client is the latest version, no need to update! '); About       End; $     End -     Else ifres = bserror_no_servconf Then -     begin -       ifSelf.manualcheck Then A       begin +ShowMessage ('The update server address is empty! '); the       End; -     End $     Else ifres ='2'  Then theSendMessage (Mainfromhandle, Wm_update_ui2,0,0)//sending DLL update messages to the main thread the     Else the     begin the       Try -Sdata.lpdata: =PChar (res); inSendMessage (Mainfromhandle, WM_UPDATE_UI1,0, Cardinal (@sData));//update messages to the mainline Cheng client the       finally the //Freemem (sdata.lpdata); About       End; the     End; the   finally the  +   End; - End; the Bayi End.
1 {thread A receives messages}2 protected3     procedureWndProc (varMessage:tmessage);Override;4 5 //Create thread B6Upgradeappthread: = Tupgradeappthread.Create(Appfiledir, Manualcheck);//will check for updates to be executed on the thread7Upgradeappthread.mainfromhandle: =Self.handle;8 9 Ten procedureTfrmmain.wndproc (varMessage:tmessage);//Receive thread Messages One var A param:tstringlist; - httpclient:tidhttp; -Controlurl, Res:string; theAppPath:string; - cdds:tcopydatastruct; - begin -Res: ="'; +    CaseMessage.msg of -WM_UPDATE_UI1:{Client Update Messages} +       begin ARes: ='Booz Client assistant needs to update to the latest version, need to shut down the client to update, is it updated immediately? '; atCDDs: =pcopydatastruct (message.lparam) ^; -AppPath: =Strpas (PChar (Cdds.lpdata)); -Controlurl: ='/ http'+ ServerIP +':'+ IntToStr (Strtoint (serverport) +2) +'/upgrademainapp?fp='+AppPath; -       End; -WM_UPDATE_UI2:{DLL Update message} -       begin inRes: ='client component Updates detected, the Booz client assistant needs to be restarted to update, is it immediately updated? '; -Controlurl: ='/ http'+ ServerIP +':'+ IntToStr (Strtoint (serverport) +2) +'/controlmainapp?ct=2'; to       End; +   Else -     inherited; the   End; *   ifRes <>"'  Then $   beginPanax Notoginseng     Try -       ifMessageBox (Self.handle, PChar (res),'Ask', mb_iconquestion + Mb_yesno + mb_topmost + mb_defbutton2) = Idyes Then the       begin +Param: = tstringlist.Create; AHttpClient: = Tidhttp.Create; theControlurl: = StringReplace (Controlurl,' ','%20', [Rfreplaceall]); + httpclient.post (Controlurl, param); -       End; $     finally $ Httpclient.free; - param. Free; -     End; the   End; - End;

Sending messages between Delphi threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.