C
C#6mo ago
!Rushaan

An event running every few seconds, want it to run only once

I'm making a WPF application and for that using GeoCoordinateWatcher but the GeoCoordinateWatcher event is supposed to run only once until I want it to run again, but it automatically runs every seconds and i have no idea why. The event name is SettingWeatherData Keep in mind im a beginner and dont have a lot of idea bout events & delegates https://pastebin.com/81UBJjPC
4 Replies
Anu6is
Anu6is6mo ago
SettingWeatherData is not your event, that's your event handler. The event you subscribe to is the PositionChanged event which I imagine is triggered everytime the position changes
Cattywampus
Cattywampus6mo ago
Make a boolean property and add a check for that property before executing the rest of the function.
public bool blockExecution {get;private set}

private void SettingWeatherData(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if(blockExecution)
return;

//Do the rest of the code here
}
public bool blockExecution {get;private set}

private void SettingWeatherData(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if(blockExecution)
return;

//Do the rest of the code here
}
Anu6is
Anu6is6mo ago
I would look into the other options that the API provides and see if there's a better way to achieve what you want. For example, there is a position property that you can call whenever you want instead of relying on the event. There is also a MovementThreshold property that can be used to limit how often the position changes event fires (places a dependency on the distance moved)
!Rushaan
!Rushaan6mo ago
ik im too late to respond but i fixed it soon after by unsubscribing from the event handler, ty for trying to help tho