Firemonkey - Indy UDP广播

时间:2015-04-22 14:26:54

标签: firemonkey indy broadcast

具有TIDUDPServer实例的给定类:

unit udpbroadcast_fm;

TUDPBC_FM = class( TObject )
protected
  IdUDPServer: TIdUDPServer; 
  Timer: TTimer;
  ...
  procedure IdUDPServerUDPRead( AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle );
  procedure TimerOnTimer( Sender: TObject );
public
  constructor Create;
  function SendDiscover: integer;  
properties  
  ...
end;

function TUDPBC_FM.SendDiscover: integer;  
begin
...
IdUDPServer.Broadcast( udpDiscovery, BCport );
...
end;

我正在使用此类发送UDP广播消息。我的问题是如何从“ Timer ”的onTimer事件处理程序(' TimerOnTimer ')中“发出信号”回到表单/自定义类实例(定义为< em> TUDPBC_FM 字段)?

定时器的间隔设置为2000毫秒,因此所有设备都有两秒钟的时间来响应广播,然后我想向表单或类实例发送信号。

在我的VCL应用程序中,我正在使用消息,但现在我正在使用firemonkey。

也许唯一的方法是使用另一种方法?例如,将计时器作为表单的字段?)。

unit mstcc_fm;

Tmstcc = class(TObject)
protected
  Fudpbc : TUDPBC_FM;
  ...
public
  function msts_Discover: integer; 
  ...
end;

function Tmstcc.msts_Discover: integer;    
begin
  ...
  Fudpbc.SendDiscover;
  ...
end;

表格单位:

unit main_fm;
...
procedure TfrmMain.btnDiscoverClick(Sender: TObject);
begin
  mstcc.msts_Discover;
  ...
end;

1 个答案:

答案 0 :(得分:1)

  

我怎样才能发出信号&#39;返回到&#39;定时器&#39;的onTimer事件处理程序(&#39; TimerOnTimer&#39;)中的表单/自定义类实例(定义为TUDPBC_FM字段)?

您可以使用TThread.Queue(),例如:

procedure TUDPBC_FM.NotifyProc;
begin
  // do something...
end;

procedure TUDPBC_FM.TimerOnTimer(Sender: TObject);
begin
  TThread.Queue(NotifyProc);
end;

procedure TUDPBC_FM.TimerOnTimer(Sender: TObject);
begin
  TThread.Queue(
    procedure
    begin
      // do something...
    end
  );
end;

TIdNotify

procedure TUDPBC_FM.NotifyProc;
begin
  // do something...
end;

procedure TUDPBC_FM.TimerOnTimer(Sender: TObject);
begin
  TIdNotify.NotifyMethod(NotifyProc);
end;

type
  TMyNotify = class(TIdNotify)
  protected
    procedure DoNotify; override;
  end;

procedure TMyNotify.DoNotify;
begin
  // do something...
end;

procedure TUDPBC_FM.TimerOnTimer(Sender: TObject);
begin
  TMyNotify.Create.Notify;
end;
相关问题