简体   繁体   English

每x秒后在listview上填充项目

[英]Populate items on listview after each x seconds

I have a method that detects beacons after each 30 seconds. 我有一种方法,每30秒检测一次信标。 The scenario is as follows: 场景如下:

  1. The scan period of beacons is 60 seconds. 信标的扫描周期为60秒。
  2. The waiting period before the next scan is 30 seconds. 下次扫描之前的等待时间为30秒。

The above settings have already been defined in the BeaconDectector class. 以上设置已经在BeaconDectector类中定义。 Now to display the beacons scanned on my UI, I have the following: 现在要显示在我的UI上扫描的信标,我有以下内容:

 private void Button_Clicked(object sender, EventArgs e)
 {       
    bool start = true; 
    while (start) {
       beaconsFound = StartDetection(); 
                Device.BeginInvokeOnMainThread(() =>
                  {
                      listView.ItemsSource = beaconsFound;

                  });
    }
 }

The StartDetection contains the logic that take place every 30 seconds and the scan period is 60 seconds. StartDetection包含每30秒发生一次的逻辑,扫描周期为60秒。 How can I ensure that I don't miss any beacons to get displayed on my listview? 如何确保我不会错过任何要显示在列表视图中的信标?

Please advise. 请指教。 Thank you. 谢谢。

You should definitely not use active waiting while(start) to manage this. 您绝对不应该使用主动等待while(start)来管理它。 A better solution would be a Timer . 更好的解决方案是使用Timer You can see the basics for this in this blogpost . 您可以在此博客文章中看到有关此内容的基础知识。

Furthermore, ensure you are not running any long running code on the UI thread (which is what Button_Clicked runs on). 此外,请确保您没有在UI线程上运行任何长时间运行的代码( Button_Clicked在其上运行)。 Ideally, your timer should fire each 30 seconds, start a new Task for the detection and use Device.BeginInvokeOnMainThread to update the list when appropriate. 理想情况下,计时器应每30秒触发一次,启动一个新的Task进行检测,并在适当时使用Device.BeginInvokeOnMainThread更新列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM