日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Delphi中多線程用消息實現(xiàn)VCL數(shù)據(jù)同步顯示

 容心居 2019-06-25
復(fù)制代碼
Delphi中多線程用消息實現(xiàn)VCL數(shù)據(jù)同步顯示

Lanno Ckeeke 

2006-5-12

概述:

delphi中嚴格區(qū)分主線程和子主線程,主線程負責GUI的更新,子線程負責數(shù)據(jù)運算,當數(shù)據(jù)運行完畢后,子線程可以向主線程式發(fā)送消息,以便通知其將VCL中的數(shù)據(jù)更新。

實現(xiàn):

關(guān)鍵在于消息的發(fā)送及接收。在消息結(jié)構(gòu)Tmessage中wParam和lParam類型為Longint,而指針類型也定義為Longint,可以通過此指針來傳遞自己所感興趣的數(shù)據(jù)。如傳遞字符數(shù)組:

數(shù)組定義:


              const 

MAX_LEN = 260;

              szName : array[0..MAX_LEN] of Char;
 

消息發(fā)送方式:

              PostMessage(Form1.Handle,WM_UPDATEDATA,Integer(PChar(@szName)),0);

消息接收方式:


              procedure TForm1.WMUpdateData(var msg : TMessage);

begin

       self.ShowData.Items.Add(PChar(msg.WParam));

end;
 

 

子線程中定義兩個數(shù)據(jù)類型:


 public

     szName : array[0..MAX_LEN] of Char;

    nIndex : Integer;
 

 

完整代碼:

子線程類:


unit TChildThread;

 

interface

 

uses

 Classes,Messages,Windows,SysUtils;//添加的使用文件

 

const MAX_LEN = 260;

type

 TChildThreads = class(TThread)

 private

    { Private declarations }

 protected

    procedure Execute; override;

 public

      szName : array[0..MAX_LEN] of Char;

    nIndex : Integer;

 end;

 

implementation

uses

       Unit1;

 

{ Important: Methods and properties of objects in VCL or CLX can only be used

 in a method called using Synchronize, for example,

 

      Synchronize(UpdateCaption);

 

 and UpdateCaption could look like,

 

    procedure TChildThread.UpdateCaption;

    begin

      Form1.Caption := 'Updated in a thread';

    end; }

 

{ TChildThread }

 

procedure TChildThreads.Execute;

begin

 { Place thread code here }

   PostMessage(Form1.Handle,WM_UPDATEDATA,Integer(PChar(@szName)),0);

   //注意類型轉(zhuǎn)化部分的寫法

end;

 

end.
 

主線程代碼:


unit Unit1;

 

interface

 

uses

 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

 Dialogs, StdCtrls, ComCtrls;

 

const

       WM_UPDATEDATA = WM_USER + 100;//自定義的消息,利用此消息來實現(xiàn)對GUI的更新

type

 TForm1 = class(TForm)

    ControlPannel: TGroupBox;

    StartThreads: TButton;

    TabControl1: TTabControl;

    SingleThread: TRadioButton;

    MultipleThreads: TRadioButton;

    StopThreads: TButton;

    ShowData: TListBox;

    ShowError: TListBox;

    Initialize: TButton;

Cleanup: TButton;

//GUI更新的消息處理函數(shù)聲明

     procedure WMUpdateData(var msg : TMessage);message WM_UPDATEDATA;

    procedure StartThreadsClick(Sender: TObject);

 private

    { Private declarations }

 public

    { Public declarations }

 end;

 

var

 Form1: TForm1;

 

implementation

uses

       TChildThread;

 

{$R *.dfm}

//消息函數(shù)定義

procedure TForm1.WMUpdateData(var msg : TMessage);

begin

       self.ShowData.Items.Add(PChar(msg.WParam));

end;

 

procedure TForm1.StartThreadsClick(Sender: TObject);

var

       oChildThread : array[0..1000] of TChildThreads;

 i : Integer;

begin

       For i := 0 to 1000 do

 begin

     oChildThread[i] := TChildThreads.Create(true);

    //向VCL中發(fā)送的數(shù)據(jù)

    strcopy(@oChildThread[i].szName,PChar('Child' + IntToStr(i)));

    oChildThread[i].nIndex := i;

    oChildThread[i].Resume;

 end;

 

end;

 

end. 
復(fù)制代碼

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多