我想从异步任务中返回一个字符串结果。
System.Threading.Tasks.Task.Run(async () => await audatex.UploadInvoice(assessment, fileName));
public async Task UploadInvoice(string assessment, string fileName)
{
//Do stuff
return string;
}
异步编程让我感到困惑,有人能解释一下吗?
我想从异步任务中返回一个字符串结果。
System.Threading.Tasks.Task.Run(async () => await audatex.UploadInvoice(assessment, fileName));
public async Task UploadInvoice(string assessment, string fileName)
{
//Do stuff
return string;
}
异步编程让我感到困惑,有人能解释一下吗?
异步编程可能需要一些时间来理解,所以我会发布对我有用的内容,以便帮助其他人。
如果你想将业务逻辑与异步代码分开,则可以使你的UploadInvoice方法不包含异步:
private string UploadInvoice(string assessment, string filename)
{
// Do stuff
Thread.Sleep(5000);
return "55";
}
然后,您可以创建一个异步包装器:
private async Task<string> UploadInvoiceAsync(string assessment, string filename)
{
return await Task.Run(() => UploadInvoice(assessment, filename));
}
给你选择称呼的权利:
public async Task CallFromAsync()
{
string blockingInvoiceId = UploadInvoice("assessment1", "filename");
string asyncInvoiceId = await UploadInvoiceAsync("assessment1", "filename");
}
有时候,您可能需要从非异步方法调用异步方法。// Call the async method from a non-async method
public void CallFromNonAsync()
{
string blockingInvoiceId = UploadInvoice("assessment1", "filename");
Task<string> task = UploadInvoiceAsync("assessment1", "filename");
string invoiceIdAsync = task.GetAwaiter().GetResult();
}
----编辑:由于人们发现这很有用,所以添加更多示例----
有时您想等待任务完成,或在完成后使用一个方法继续任务。以下是您可以在控制台应用程序中运行的工作示例。
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));
}
}
Task<string>
,而不是 Task
:public async Task<string> UploadInvoice(string assessment, string fileName)
{
//Do stuff
return "some string";
}
string result = await UploadInvoice("", "")
- Ricpublic async Task<string> UploadInvoice(string assessment, string fileName)
{
string result = GetResultString();//Do stuff
return Task.FromResult(result);
}
请执行以下步骤:
public async Task<string> UploadInvoice(string assessment, string fileName)
然后等待结果:
string result = await UploadInvoice("", "");
更多示例可以在这里查看:
async
方法中调用它,我猜你没有这样做?尝试以下代码:var myTask = Task.Run(() => a.UploadInvoice("", "")); string result = myTask.Result;
- Ric
Task
改为Task<string>
。 - Charles Mager