BlackLotus
BlackLotus

Reputation: 553

Task.ContinueWith() is not working after the Task.Run()

Im doing with the activity indicator of the xamarin. I want to stop the indicator after the product is get.

protected override void OnAppearing()
    {
        base.OnAppearing();

        if (!_appeared) // Avoid repeat loding
        {
            activity.IsEnabled = true;
            activity.IsRunning = true;
            activity.IsVisible = true;

            var task = Task.Run(() =>
            {
                ProductViewData productViewData = new ProductViewData();
                products = productViewData.GetProductList("10");
                count = 10;
                productListView.ItemsSource = products;
            });

            task.ContinueWith(t =>
            {
                activity.IsEnabled = false;
                activity.IsRunning = false;
                activity.IsVisible = false;
            });

            _appeared = true;
        }
    }

Thanks for any help.

Upvotes: 1

Views: 853

Answers (2)

canton7
canton7

Reputation: 42235

Code like this is a lot easier with async/await:

protected override async void OnAppearing()
{
    base.OnAppearing();

    if (!_appeared) // Avoid repeat loding
    {
        _appeared = true;
        activity.IsEnabled = true;
        activity.IsRunning = true;
        activity.IsVisible = true;

        var products = await Task.Run(() =>
        {
            ProductViewData productViewData = new ProductViewData();
            return productViewData.GetProductList("10");
        });

        productListView.ItemsSource = products;

        activity.IsEnabled = false;
        activity.IsRunning = false;
        activity.IsVisible = false;
    }
}

Although the code is simple and readable, there's a lot going on behind the scenes, particularly around when this method returns, and what thread the code after the await is run on. It is well worth reading up on this.

Upvotes: 3

Hasan
Hasan

Reputation: 1298

I guess this is what you need.

var task = Task.Run(delegate { Console.WriteLine("Task started!"); })
           .ContinueWith(delegate { Console.WriteLine("Task continued"); });

Just wrap what you wanna do as functions and call that functions in your task steps. For more information read here.

Edit:

Since an UI operation is the subject of this question, I thought this code from the page I linked would be more useful. This code utilizes UI and background scheduler.

private void Button1_Click(object sender, EventArgs e)  
{  
   var backgroundScheduler = TaskScheduler.Default;  
   var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();  
   Task.Factory.StartNew(delegate { DoBackgroundComputation(); },  
                         backgroundScheduler).  
   ContinueWith(delegate { UpdateUI(); }, uiScheduler).  
                ContinueWith(delegate { DoAnotherBackgroundComputation(); },  
                             backgroundScheduler).  
                ContinueWith(delegate { UpdateUIAgain(); }, uiScheduler);  
}  

Upvotes: 1

Related Questions