Implement a timeout function

 

This code above, we find a problem, ah, if you want to access the link for a long time, then it is also very hurt ah.
Whether to join a timeout, if the results did not return visit for some time, then the resources left to other in need.

public static async Task<string> visitTimeoutUrl(HttpClient client,string url) { var visitTask=client.GetStringAsync(url); var timeoutTask = Task.Delay(3000); var completedTask = await Task.WhenAny(visitTask,timeoutTask); if (completedTask == timeoutTask) { return null; } return await visitTask; }

Guess you like

Origin www.cnblogs.com/robertyao/p/12655527.html