• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi线程间发送消息

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

转载自:https://www.cnblogs.com/ADAN1024225605/p/7927695.html

线程B向线程A发送消息,SendMessage(线程A句柄, msg, wParam参数, lParam参数);

线程A重写WndProc(var Message: TMessage);

{ 线程B发送消息 }
unit ThreadUnit;

interface

uses
  Classes, ActiveX, businessService, Dialogs, Constant, Windows, Messages, SysUtils;

type
  TUpgradeAppThread = class(TThread)
  protected
    procedure execute; override;
  private
    appFileDir: string;
    manualCheck: Boolean;
  public
    MainFromHandle: HWND;  //主线程句柄
    constructor Create(appFileDir: string; manualCheck: Boolean);
  end;

const {0x0400 - 0x7FFF}
  WM_UPDATE_UI1 = WM_USER + $1001;
  WM_UPDATE_UI2 = WM_USER + $1002;

var
  upgradeAppThread: TUpgradeAppThread;

implementation

constructor TUpgradeAppThread.Create(appFileDir: string; manualCheck: Boolean);
begin
  inherited Create(False);
  Self.appFileDir := appFileDir;
  Self.manualCheck := manualCheck;
end;

procedure TUpgradeAppThread.Execute;
var
  res: string;
  sData: TCopyDataStruct;
begin
  FreeOnTerminate := True;
  try
    res := upgradeApplication(appFileDir, False);
    if res = '0' then
    begin
      //未检测到完整版客户更新才进行dll更新检测
      res := upgradeDllOnline(appFileDir);
    end;

    if res = '0' then
    begin
      if Self.manualCheck then
      begin
        ShowMessage('博思客户端是最新版本,无需更新!');
      end;
    end
    else if res = BSERROR_NO_SERVCONF then
    begin
      if Self.manualCheck then
      begin
        ShowMessage('更新服务器地址为空!');
      end;
    end
    else if res = '2' then
      SendMessage(MainFromHandle, WM_UPDATE_UI2, 0, 0)  //向主线程发送dll更新消息
    else
    begin
      try
        sData.lpData := PChar(res);
        SendMessage(MainFromHandle, WM_UPDATE_UI1, 0, Cardinal(@sData));   //向主线程发客户端更新消息
      finally
//        FreeMem(sData.lpData);
      end;
    end;
  finally

  end;
end;

end.

  

{ 线程A接收消息 }
protected
    procedure WndProc(var Message: TMessage); override;

//创建线程B
upgradeAppThread := TUpgradeAppThread.Create(appFileDir, manualCheck);   //将检查更新放在线程中执行
  upgradeAppThread.MainFromHandle := Self.Handle;


procedure TfrmMain.WndProc(var Message: TMessage);  //接收线程消息
var
  param: TStringList;
  httpClient: TIdHTTP;
  controlUrl, res: string;
  appPath: string;
  cdds: TcopyDataStruct;
begin
  res := '';
  case Message.Msg of
    WM_UPDATE_UI1:   { 客户端更新消息 }
      begin
        res := '博思客户端助手需要更新到最新版本,需要关闭客户端才能更新,是否立即更新?';
        cdds := PcopyDataStruct(Message.LParam)^;
        appPath := StrPas(PChar(cdds.lpData));
        controlUrl := 'http://' + serverIp + ':' + IntToStr(StrToInt(serverPort) + 2) + '/upgradeMainApp?fp=' + appPath;
      end;
    WM_UPDATE_UI2:  { dll更新消息 }
      begin
        res := '检测到客户端组件更新,博思客户端助手需要重启才能更新,是否立即更新?';
        controlUrl := 'http://' + serverIp + ':' + IntToStr(StrToInt(serverPort) + 2) + '/controlMainApp?ct=2';
      end;
  else
    inherited;
  end;
  if res <> '' then
  begin
    try
      if MessageBox(Self.Handle, PChar(res), '询问', MB_ICONQUESTION + MB_YESNO + MB_TOPMOST + MB_DEFBUTTON2) = IDYES then
      begin
        param := TStringList.Create;
        httpClient := TIdHTTP.Create;
        controlUrl := StringReplace(controlUrl, ' ', '%20', [rfReplaceAll]);
        httpClient.Post(controlUrl, param);
      end;
    finally
      httpClient.Free;
      param.Free;
    end;
  end;
end;

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi teechrt中TChart 一些属性设置发布时间:2022-07-18
下一篇:
delphi控制Excel-delphi研究发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap