Is there a way to start a task at app start and then when it finishes get notified

Lloyd Sheen 1,481 Reputation points
2022-12-15T16:39:09.963+00:00

I have a set of code that takes a while (600 endpoint calls) and it will create files with the results of the task. At present I run it on demand and it takes about 2-3 minutes to run. What I would like to do is start a task at app start and when the task is finished get notified. Another option would be to start the task and if they need/want the data they could check the Task status.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,594 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 5,537 Reputation points Microsoft Employee
    2022-12-16T02:17:11.917+00:00

    There isn't much MAUI specific on this - your asynchronicity will all happen at the base .Net / C# level.

    Exact details will depend on your specific scenario: if your task is serialized or parallelizable, if it's I/O bound or CPU bound, how your application is laid out, etc.

    To get started, look at the Asynchronous programming with async and await docs in the C# programming guide. It sounds like you have an I/O bound task that will work well with the Task-based Asynchronous Pattern (TAP).

    When the task complete you can fire a completion event to let some other part of the system know that it's done. Again, specific details here will be very application specific.

    Since it sounds like this is part of your data model initialization, you might trigger the initialization task when you create the model and then raise PropertyChanged or custom events when the data is available, depending on how the app expects to use this data. You may make different choices if the app just need to let the user know that the files are created than if the app needs to expose the data in a View once its available.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Lloyd Sheen 1,481 Reputation points
    2022-12-17T22:15:33.897+00:00

    Thanks Rob, This provided what I needed to get a prototype that I can use in my app.

    I am going to post the code in case anyone else would like to do something similar. The app is simple with a button to start the process and a button to wait for the task to end. It has Console.Writes to show that what I want is happening .

    using System.Threading;

    namespace TestAsync;

    public partial class MainPage : ContentPage
    {
    int count = 0;

                public MainPage()  
                {  
                                InitializeComponent();  
                }  
    
    
    // This is the task that I will wait on  
    Task theTask;  
      
    
    // This button click starts the async task  
    private async void StartTest(object sender, EventArgs e)  
    {  
        Console.WriteLine("********************** Calling the asyn");  
    
        theTask = DoManyWebQueries();  
    
        Console.WriteLine("********************** Called the async");  
    
    }  
    
    // This button click will wait for the async task to complete  
    private async void WaitForTask(object sender, EventArgs e)  
    {  
        Console.WriteLine("********************** Start waiting");  
        await theTask;  
        Console.WriteLine("********************** Waiting finished");  
    }  
    
    
    // This is the async task which basically does about a 100  
    // web queries so that it will take a while before it completes  
    public async Task<int> DoManyWebQueries()  
    {  
    
        Console.WriteLine("********************** Task is starting");  
        var client = new HttpClient();  
    
        string theQuery = "https://statsapi.web.nhl.com/api/v1/teams/ID?expand=team.roster";  
          
        for (int x=0;x<10;x++)  
        {  
    
            for (int i = 1; i < 10; i++)  
            {  
                string query=theQuery.Replace("ID",i.ToString());  
    
                Console.WriteLine("********************** " + query);  
    
                Task<string> getStringTask =  
                    client.GetStringAsync(query);  
    
                string contents = await getStringTask;  
            }  
    
        }  
    
        Console.WriteLine("********************** Task is ending");  
    
        return 0;  
    }  
    

    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.