红鱼儿

uniGUI应用检测无人操作

在浏览器中运行一个uniGUI应用,当超过一定时间,无人操作时,我们想实现自动退出应用,该如何实现呢?

下面是具体的实现方法:

在MainForm中,增加了一个事件OnSessionIdle,当超过一定时间无人操作时,会触发这个事件,这个时间在哪里设置呢?答案是UniServerModule的AjsxTimeout这个属性,单位毫秒。

利用上面的原理,我们开始实现一个超过30秒无人操作时,自动退出应用。

1.设置UniServerModule.AjaxTimeout:=30000

2.实现MainForm.OnSessionIdle事件:

在这个事件中,我们显示一个提示窗口UniIdleForm,当这个窗口不返回mrOK时,关闭应用。

procedure TMainForm.UniFormSessionIdle(Sender: TObject);
begin
  UniIdleForm.ShowModal(
    procedure(Sender: TComponent; AResult:Integer)
    begin
      if AResult <> mrOK then
      begin
        UniSession.Terminate('Your session terminated because it was idle for 30 seconds!');
      end;
    end
  );
end;

3.接下来,看UniIdleForm的实现:

建立一个UniIidleForm,放置一个UniTimer1,设置Interval=5000,即5秒触发一次。

建立一个私有变量FCnt,初始值设置为6,在UniTimer1的OnTimer事件中,利用FCnt变量,控制计时。当FCnt小等于0时,自动退出窗口并设置返回值为mrCancel。具体的代码:

procedure TUniIdleForm.UniTimer1Timer(Sender: TObject);
begin
  if FCnt <= 0 then
  begin
    ModalResult := mrCancel;
    Exit;
  end;
  UniLabel1.Caption := 'Session will be terminated in <b>' +
                      IntToStr(FCnt * 5) + '</b> seconds.<br>' +
                      'Please press "Continue" to return to your session.';

  Dec(FCnt);
end;

本文基于官方的例子Session Idle Timeout所写,具体项目在FMSoft\Framework\uniGUI\Demos\Desktop\Session Idle Timeout目录。

 

posted on 2021-10-09 10:49  红鱼儿  阅读(821)  评论(0编辑  收藏  举报