Controlling async task behaviour from standard page

DinoNovak.3412DinoNovak.3412 USMember ✭✭
edited October 2017 in Xamarin.Forms

Hi all,
I have a regular page that contains content that I want to refresh periodacially.
To support this from main page I am executing async task

    public partial class pageMain : ContentPage
    {
    Task _refreshList;     

    public pageMain()
        {
                InitializeComponent();
        _refreshList = RefreshList();
    }
   }

and task is defined as:

//timer for periodic refreshes
async Task RefreshList()
{
        while (true)
        {
    ...
    await Task.Delay(3000);
    }
await Task.Delay(3000);
}

this is all working propoery and I am getting desired behaviour, what i would like to have is option to suspend task if application is send to backround (as it is pooling web service it does not make sense to do it all the time in the background too)

Actually after some time this task is either suspended or crashes, so when user gets app I need to reinitiate it, this is what I would like to do it myself instead to wait for it to stop on its own. Also when app is brought up I would like to re-initialize the task again.

As this task is updating gui by checking some gui object statuses I am unable to use background task in the app main part.

What is the best approach for it?

Thanx,
D

Posts

  • DinoNovak.3412DinoNovak.3412 USMember ✭✭

    So I figured out how to use MessagingCenter and subscribe to notifications to propagate OnSleep and onResume events to the opened content page.

    But I am struggling with controlling the Async task. Looks like that after phone is put in sleep mode this task is first frozen, and then probably terminated/canceled after some time.

    I would appreciate help how to use the async task and have full control over its lifecycle. I have a feeling that I may be starting multiple ones.

    Regards,
    D

Sign In or Register to comment.