C# - Refresh method periodically

drizzle0815

I want to refresh my UWP-UI periodically after like 5 minutes. I have a Method "Page_Loaded" where all information from the classes is sent to the UI-Elements. So if i refresh this method, the UI would do too, right?

The Code is like this:

private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            RootObject myWeather = await Openweathermap.GetWeather();
            string icon = String.Format("http://openweathermap.org/img/wn/{0}@2x.png", myWeather.weather[0].icon);
            ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));

            TempTextBlock.Text = ((int)myWeather.main.temp).ToString() + "°";
            DescriptionTextBlock.Text = myWeather.weather[0].description;
            LocationTextBlock.Text = myWeather.name;

            var articlesList = NewsAPI.GetNews().Result.articles;
            lvNews.ItemsSource = articlesList;

            Welcometxt.Text = MainPage.WelcomeText();
        }

So, how do I refresh this method after 5 Minutes, so that it gets the new information and sends it to the UI?

Richard Zhang - MSFT

So, how do I refresh this method after 5 Minutes, so that it gets the new information and sends it to the UI?

Repeatedly calling the Page_Loaded method is not a recommended practice, the recommended approach is to use DispatcherTimer, a timer within the UI thread.

We can extract the code inside Page_Loaded as a function.

private DispatcherTimer _timer;
public MainPage()
{
    this.InitializeComponent();
    _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMinutes(5);
    _timer.Tick += Timer_Tick;
}

private async Task GetData()
{
    RootObject myWeather = await Openweathermap.GetWeather();
    string icon = String.Format("http://openweathermap.org/img/wn/{0}@2x.png", myWeather.weather[0].icon);
    ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));

    TempTextBlock.Text = ((int)myWeather.main.temp).ToString() + "°";
    DescriptionTextBlock.Text = myWeather.weather[0].description;
    LocationTextBlock.Text = myWeather.name;

    var articlesList = NewsAPI.GetNews().Result.articles;
    lvNews.ItemsSource = articlesList;

    Welcometxt.Text = MainPage.WelcomeText();
}

private async void Timer_Tick(object sender, object e)
{
    await GetData();
}

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    await GetData();
    _timer.Start();
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    _timer.Stop();
    base.OnNavigatedFrom(e);
}

With DispatcherTimer.Tick, we can execute tasks regularly, and when we leave the page, we can stop the timer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Spring Boot application run method periodically

From Java

Execute method periodically in the Vaadin GUI toolkit

From Dev

Periodically refresh IMPORTXML() spreadsheet function

From Dev

How to wake a C++ 11 thread periodically?

From Dev

How to periodically flush c# FileStream to the disk?

From Dev

refresh materialized view periodically postgres

From Dev

Periodically writing to a pipe read by child process in C

From Dev

How to use the after method to make a callback run periodically?

From Dev

Refresh Flutter Text widget content every 5 minutes or periodically

From Dev

Javascript auto scroll page up and down and refresh the data periodically

From Dev

How can I inject JavaScript code to periodically refresh a page

From Dev

Auto Refresh Portlet Liferay 6.0(Periodically refresh)

From Dev

How to refresh full page periodically

From Dev

Call a method periodically without using Runnable and Timer class

From Dev

i want to launch a class method periodically using spring

From Dev

Refresh core-ajax periodically using setTimeout and go();

From Dev

Periodically refill BufferedWaveProvider with NAudio (C#)

From Dev

Should HDDs be wiped out periodically to refresh them?

From Dev

Change Tab Colour and Periodically Refresh

From Dev

C# Proper way of periodically firing functions

From Dev

Spark Streaming: How to periodically refresh cached RDD?

From Dev

How long can a Chromebook periodically refresh a page?

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

How to refresh ListView in ContentPage in xamarin forms periodically

From Dev

How to call a method / function periodically? (time.sleep fails)

From Dev

How can I periodically refresh in Zeppelin with Spark? (Java)

From Dev

Change refresh interval for Openstack provider in CloudForms to run periodically

From Dev

EntityManager refresh() method does not refresh but resets

From Dev

Periodically refresh Kotlin flow (polling)

Related Related

  1. 1

    Spring Boot application run method periodically

  2. 2

    Execute method periodically in the Vaadin GUI toolkit

  3. 3

    Periodically refresh IMPORTXML() spreadsheet function

  4. 4

    How to wake a C++ 11 thread periodically?

  5. 5

    How to periodically flush c# FileStream to the disk?

  6. 6

    refresh materialized view periodically postgres

  7. 7

    Periodically writing to a pipe read by child process in C

  8. 8

    How to use the after method to make a callback run periodically?

  9. 9

    Refresh Flutter Text widget content every 5 minutes or periodically

  10. 10

    Javascript auto scroll page up and down and refresh the data periodically

  11. 11

    How can I inject JavaScript code to periodically refresh a page

  12. 12

    Auto Refresh Portlet Liferay 6.0(Periodically refresh)

  13. 13

    How to refresh full page periodically

  14. 14

    Call a method periodically without using Runnable and Timer class

  15. 15

    i want to launch a class method periodically using spring

  16. 16

    Refresh core-ajax periodically using setTimeout and go();

  17. 17

    Periodically refill BufferedWaveProvider with NAudio (C#)

  18. 18

    Should HDDs be wiped out periodically to refresh them?

  19. 19

    Change Tab Colour and Periodically Refresh

  20. 20

    C# Proper way of periodically firing functions

  21. 21

    Spark Streaming: How to periodically refresh cached RDD?

  22. 22

    How long can a Chromebook periodically refresh a page?

  23. 23

    JNI: Calling a java method from C periodically is not working

  24. 24

    How to refresh ListView in ContentPage in xamarin forms periodically

  25. 25

    How to call a method / function periodically? (time.sleep fails)

  26. 26

    How can I periodically refresh in Zeppelin with Spark? (Java)

  27. 27

    Change refresh interval for Openstack provider in CloudForms to run periodically

  28. 28

    EntityManager refresh() method does not refresh but resets

  29. 29

    Periodically refresh Kotlin flow (polling)

HotTag

Archive