gpt4 book ai didi

c# - C# 示例中的异步和等待

转载 作者:行者123 更新时间:2023-11-30 12:38:23 24 4
gpt4 key购买 nike

我从 Microsoft 网站上找到了以下关于 Async 和 await 的示例( https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ ):

// Three things to note in the signature:  
// - The method has an async modifier.
// - The return type is Task or Task<T>. (See "Return Types" section.)
// Here, it is Task<int> because the return statement returns an integer.
// - The method name ends in "Async."
async Task<int> AccessTheWebAsync()
{
// You need to add a reference to System.Net.Http to declare client.
HttpClient client = new HttpClient();

// GetStringAsync returns a Task<string>. That means that when you await the
// task you'll get a string (urlContents).
Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();

// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string urlContents = await getStringTask;

// The return statement specifies an integer result.
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.
return urlContents.Length;
}

我的第一个问题来自网页“如果 AccessTheWebAsync 在调用 GetStringAsync 和等待其完成之间没有任何它可以做的工作,您可以通过在以下单个语句中调用和等待来简化代码。”似乎暗示以下语句确实在做某事:

    Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");  

这是真的吗?我虽然它只是一个任务的定义,但它还没有开始/运行。

第二个问题是我尝试如下更改实现以检查我的想法:

async Task<int> AccessTheWebAsync()  
{
HttpClient client = new HttpClient();

//Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");
Task<string> getStringTask = DoPriorWork();

// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();

string urlContents = await getStringTask;

return urlContents.Length;
}
void DoIndependentWork()
{
resultsTextBox.Text += "Working . . . . . . .\r\n";
}

Task<string> DoPriorWork()
{
return new Task<string>(
() =>
{
string retStr = "good";
Debug.Assert(true, "running dopriorwork");
System.Threading.Thread.Sleep(10000);
return retStr;
}
);
}

但是,方法 DoPriorWork() 根本没有执行。这个实现有什么问题?谢谢!

最佳答案

Is that true? I thought it's just a definition of a task, but it has not yet started/run.

的确如此。您调用的操作立即返回,给您的任务是“热的”——即已经在运行。这就是您实际获得任务的原因,因此您可以稍后等待它。因此,您可以获得一种运行其他东西的方法,如示例所述。

Second question is I tried to change the implementation as follows to check my idea...

为了能够让代码运行,请尝试按如下方式更改方法 DoPriorWork:

private Task<string> DoPriorWork()
{
return Task.Run(() =>
{
var retStr = "good";
Console.WriteLine("running dopriorwork");
Thread.Sleep(10000);
return retStr;
});
}

请注意,我正在排队并返回一个“热"任务,使用静态方法 Task.Run()

希望这对您有所帮助!

关于c# - C# 示例中的异步和等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com