How to return the result from an asynchronous task?

I want to return the result of a string from an async task.

System.Threading.Tasks.Task.Run(async () => await audatex.UploadInvoice(assessment, fileName));

public async Task UploadInvoice(string assessment, string fileName)
{
    //Do stuff
    return string;
}

Asynchronous programming bothers me, can someone explain this?

+4
source share
4 answers

Your method should return Task<string>, not Task:

public async Task<string> UploadInvoice(string assessment, string fileName)
{
    //Do stuff
    return string;
}
+9
source

Asynchronous programming may take some time to get your head around, so I will post what was useful to me if it helps someone else.

If you want to separate business logic from asynchronous code, you can save your UploadInvoice method without async:

private string UploadInvoice(string assessment, string filename)
{
    // Do stuff    
    Thread.Sleep(5000);

    return "55";
}

Then you can create an async shell:

private async Task<string> UploadInvoiceAsync(string assessment, string filename)
{
    return await Task.Run(() => UploadInvoice(assessment, filename));
}

Giving you the choice to call:

public async Task CallFromAsync()
{
    string blockingInvoiceId = UploadInvoice("assessment1", "filename");

    string asyncInvoiceId = await UploadInvoiceAsync("assessment1", "filename");
}

async non-async.

// Call the async method from a non-async method
public void CallFromNonAsync()
{
    string blockingInvoiceId = UploadInvoice("assessment1", "filename");

    Task<string> task = Task.Run(async () => await UploadInvoiceAsync("assessment1", "filename"));
    task.Wait();
    string invoiceIdAsync = task.Result;
}

---- EDIT: , ----

. , .

    class Program
    {
        static void Main(string[] args)
        {
            var program = new Program();
            program.Run();
            Console.ReadKey();
        }

        async void Run()
        {
            // Example 1
            Console.WriteLine("#1: Upload invoice synchronously");
            var receipt = UploadInvoice("1");
            Console.WriteLine("Upload #1 Completed!");
            Console.WriteLine();

            // Example 2
            Console.WriteLine("#2: Upload invoice asynchronously, do stuff while you wait");
            var upload = UploadInvoiceAsync("2");
            while (!upload.IsCompleted)
            {
                // Do stuff while you wait
                Console.WriteLine("...waiting");
                Thread.Sleep(900);
            }
            Console.WriteLine("Upload #2 Completed!");
            Console.WriteLine();

            // Example 3
            Console.WriteLine("#3: Wait on async upload");
            await UploadInvoiceAsync("3");
            Console.WriteLine("Upload #3 Completed!");
            Console.WriteLine();

            // Example 4
            var upload4 = UploadInvoiceAsync("4").ContinueWith<string>(AfterUploadInvoice);
        }

        string AfterUploadInvoice(Task<string> input)
        {
            Console.WriteLine(string.Format("Invoice receipt {0} handled.", input.Result));
            return input.Result;
        }

        string UploadInvoice(string id)
        {
            Console.WriteLine(string.Format("Uploading invoice {0}...", id));
            Thread.Sleep(2000);
            Console.WriteLine(string.Format("Invoice {0} Uploaded!", id));
            return string.Format("<{0}:RECEIPT>", id); ;
        }

        Task<string> UploadInvoiceAsync(string id)
        {
            return Task.Run(() => UploadInvoice(id));
        }
    }
+9

Do it:

public async Task<string> UploadInvoice(string assessment, string fileName)

Then the awaitresult:

string result = await UploadInvoice("", "");

Additional examples can be seen here:

Asynchronous Return Types

+2
source
public async Task<string> UploadInvoice(string assessment, string fileName)
{
    string result = GetResultString();//Do stuff    
    return Task.FromResult(result);
}
0
source

Source: https://habr.com/ru/post/1612405/


All Articles