0

I want to call a function after an interval in uwp so I have this:

 private void TimedEvent()
        {
           aTimer = new System.Timers.Timer();
            aTimer.Interval = 40000;
           aTimer.Elapsed += new ElapsedEventHandler(PresencePage_Loaded);
            aTimer.AutoReset = true;

        }

 private async void PresencePage_Loaded(object sender, RoutedEventArgs e)
        {}

However I get a no overload for presencePage_loaded matches delegate ElapsedeventHandler error. Where am I going wrong?

0

2 Answers 2

4

The signature of the ElapsedEventHandler delegate is:

public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);

You need to change your method signature to match, so you need to change RoutedEventArgs to ElapsedEventArgs:

private async void PresencePage_Loaded(object sender, ElapsedEventArgs e)
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

might cause more problems than it fixes as PresencePage_Loaded is the default for the loaded event so changing its signature will break the pages event
3

Elapsed is declared as

public event System.Timers.ElapsedEventHandler Elapsed;

where as loaded is

public event System.Windows.RoutedEventHandler Loaded;

as the events have different types you can't use the handler for one as a handler for the other

the simplest option would be to pull your logic out of the handler into a function

public async Task DoSomething(){...}

then you can have 2 handlers that call this function ie

private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    await DoSomething();
}

private async void PresencePage_Loaded(object sender, RoutedEventArgs e)
{
    await DoSomething();
}

which would be attached as follows

aTimer.Elapsed += Timer_Elapsed;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.