haunteddevil619
haunteddevil619

Reputation: 141

C# - Run code in background and continue with current method

I am new to async/await in C#. This is what I'm trying to do:

private async Task LoadSomeData()
{
    /*
    *This is where the 'Data' is loaded with HTTP
    */
    //Data loading finished. Need to return control to next line from caller function and use this 'Data'

    // Also need to use 'Data' to load 'Data2' over HTTP but is not very important
    // and hence can be run in background. No piece of code depends on this currently.
}

private async void UIEventHandler()
{
    //await until 'Data2' has loaded
    //Continue handling the event after Data2 is loaded
}

My question is, how do I achieve the above operation. I don't want to return from LoadSomeData() method after retrieving Data2 as the current operation does not depend on it.

Upvotes: 1

Views: 9152

Answers (2)

mipnw
mipnw

Reputation: 2365

private async Task LoadSomeData()
{
    var data = await Task.Delay(1000); // Replace with your HTTP GET
    return data;
}

private async Task LoadSomeData2(Data data)
{
    var data2 = await Task.Delay(1000); // Replace with your other HTTP GET
    return data2;
}

private async void UIEventHandler()
{
    var data = await LoadSomeData();
    var data2 = await LoadSomeData2(data);
}

UPDATE: I recommend you read Async/Await - Best Practices in Asynchronous Programming

Upvotes: -1

Nigel Whatling
Nigel Whatling

Reputation: 2391

Below is some quick code I threw together around your method names. I've made them static just for convenience.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Main Start");

        UIEventHandler(2000);
        UIEventHandler(500);
        UIEventHandler(1000);

        Console.WriteLine("Main Stop");

        Thread.Sleep(3000); // wait for tasks to finish
    }

    private static async Task LoadSomeData(int delay)
    {
        await Task.Run(() =>
        {
            Console.WriteLine("Start (LoadSomeData) " + delay);
            Thread.Sleep(delay);
            Console.WriteLine("Stop (LoadSomeData) " + delay);
        });
    }

    private static async Task UIEventHandler(int delay)
    {
        Console.WriteLine("Start (UIEventHandler) " + delay);
        await LoadSomeData(delay);
        Console.WriteLine("Stop (UIEventHandler) " + delay);
    }
}

This is sample output from a test run:

Main Start
Start (UIEventHandler) 2000
Start (LoadSomeData) 2000
Start (UIEventHandler) 500
Start (UIEventHandler) 1000
Main Stop
Start (LoadSomeData) 500
Start (LoadSomeData) 1000
Stop (LoadSomeData) 500
Stop (UIEventHandler) 500
Stop (LoadSomeData) 1000
Stop (UIEventHandler) 1000
Stop (LoadSomeData) 2000
Stop (UIEventHandler) 2000

You will notice that every call to UIEventHandler showed it's "Start" message straight away. However, the "Stop" message is not shown until the respective call to LoadSomeData is complete. The await operator is essentially telling the code in UIEventHandler to stop until the task in the called method is complete.

That's a really simplistic view of it, though. I'd suggest doing some reading. This might be a good start: Asynchronous programming

Upvotes: 2

Related Questions