【问题标题】:GMap.NET WPF Marker timer updateGMap.NET WPF 标记计时器更新
【发布时间】:2018-07-11 03:14:15
【问题描述】:

我很少有使用线程的经验,所以我的逻辑中可能遗漏了一些非常基本的信息。

无论如何,我正在尝试根据 1000 毫秒计时器更新 GMapMarker。

我的计时器开始:

aTimer = new System.Timers.Timer();
aTimer.Elapsed += OnTimerEvent;
aTimer.Interval = 1000;
aTimer.Enabled = true;

我的更新代码:

int _positionIncriment = 0;
private  void OnTimerEvent(object sender, System.Timers.ElapsedEventArgs e)
{
    PointLatLng p = m_debugPath[_positionIncriment];
    _gpsMarker.Position = p;
    _positionIncriment++;

    _gmap.ForceUpdateOverlays();

    if(_positionIncriment >= m_debugPath.Count)
    {
        _positionIncriment = 0;
    }
}

当我逐步执行此块时,它会在 ForceUpdateOverlays 处停止,并且不再继续前进。 据我所知,GMap 的渲染函数在它自己的线程中,这可能会导致问题。但正如我所说,对线程只有基本的了解,我有点迷失了。 任何帮助都会很棒。

【问题讨论】:

    标签: wpf multithreading gmap.net


    【解决方案1】:

    使用 DispatcherTimer 而不是 Timer。它的 Tick 事件在 UI 线程中触发:

    using System.Windows.Threading;
    ...
    
    aTimer = new DispatcherTimer();
    aTimer.Tick += OnTimerEvent;
    aTimer.Interval = TimeSpan.FromSeconds(1);
    aTimer.IsEnabled = true; // or aTimer.Start();
    ...
    
    private void OnTimerEvent(object sender, EventArgs e)
    {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      看起来我需要在运行我的代码之前调用 Dispatcher。 我不知道这是解决这个问题的“正确”方法,但最终结果是我想要的。

      希望这对其他人有所帮助。

      int _positionIncrement = 0;
      private  void OnTimerEvent(object sender, System.Timers.ElapsedEventArgs e)
      {
          this.Dispatcher.Invoke(() =>
          {
              PointLatLng p = m_debugPath[_positionIncrement];
              _gps.Position = p;
              _positionIncrement++;
      
              _gmap.ForceUpdateOverlays();
      
              if(_positionIncrement >= m_debugPath.Count)
              {
                  _positionIncrement = 0;
              }
          });
      }
      

      【讨论】:

      • 或者只使用 DispatcherTimer。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多