【问题标题】:Dart server check at intervalsDart 服务器每隔一段时间检查一次
【发布时间】:2012-11-16 15:21:22
【问题描述】:

我正在尝试每隔 5 秒运行一次“checkServer”。但是“服务器很好”只运行一次。重复该功能需要做什么?

import 'dart:io';
import 'dart:uri';
import 'dart:isolate';

checkServer() {
  HttpClient client = new HttpClient();
  HttpClientConnection connection = client.getUrl(...);

  connection.onResponse = (res) {
    ...
    print('server is fine');
    //client.shutdown();
  };

  connection.onError = ...;
}

main() {
  new Timer.repeating(5000, checkServer());
}

【问题讨论】:

    标签: dart dart-isolates


    【解决方案1】:

    您必须为Timer.repeating 构造函数提供一个void callback(Timer timer) 作为第二个参数。

    使用以下代码,checkServer 将每 5 秒调用一次。

    checkServer(Timer t) {
      // your code
    }
    
    main() {
      // schedule calls every 5 sec (first call in 5 sec)
      new Timer.repeating(5000, checkServer);
    
      // first call without waiting 5 sec
      checkServer(null);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多