GMap.NET WPF Marker timer update

I have very little experience using threads so I might be missing some pretty basic infomration in my logic.

Anyway, I'm trying to update a GMapMarker based on 1000ms timer.

My Timer start:

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

My update code:

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;
    }
}

As I step through this block, it stops at the ForceUpdateOverlays and never progresses further. As far as I can tell the render functions of GMap is within its own thread which might be causing issue.. but as I said, only have a basic understanding of threading I'm a bit lost. Any help would be great.

2 answers

  • answered 2018-07-11 04:18 megabytes

    Looks like I needed to invoke the Dispatcher before running my code. I don't know is this the the 'correct' way to solve this problem, but the end result is what i was desiring.

    Hope this helps someone else.

    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;
            }
        });
    }
    

  • answered 2018-07-11 05:41 Clemens

    Use a DispatcherTimer instead of Timer. Its Tick event is fired in the UI thread:

    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)
    {
        ...
    }