简体   繁体   English

c# 让我的特定行在 x 秒后超时 c#

[英]c# make my a specific line to timeout after x seconds c#

I have a line in C# which does not work very reliable and does not time out at all and runs for infinity.我在 C# 中有一条线,它运行起来不是很可靠,而且根本不会超时并且可以无限运行。

to be more precise i am trying to check the connection to a proxy WebClient.DownloadString更准确地说,我正在尝试检查与代理 WebClient.DownloadString 的连接

I want it to timeout after 5 seconds without making the full method asynchronous我希望它在 5 秒后超时而不使完整方法异步

so the code should be like this:所以代码应该是这样的:

bool success = false

do_this_for_maximum_5_seconds_or_until_we_reach_the_end
{
WebClient.DownloadString("testurl");
success = true;
}

it will try to download testurl and after it did download it it will set success to true.它将尝试下载 testurl 并在下载后将成功设置为 true。 If DownloadString takes more than 5 seconds, the call is canceled, we do not reach the the line where we set success to true, so it remains false and i know that it field.如果 DownloadString 花费的时间超过 5 秒,则调用被取消,我们没有到达将成功设置为 true 的行,所以它仍然是 false,我知道它是字段。

The thread will remain frozen while we try to DownloadString, so the action is not taking parallel.当我们尝试下载字符串时,线程将保持冻结状态,因此该操作不会并行进行。 The ONLY difference to a normal line would be that we set a timeout after 5 seconds与普通线路的唯一区别是我们在 5 秒后设置了超时

Please do not suggest alternatives such as using HttpClient, because i need a similar codes also for other places, so i simply want a code which will run in a synchronous application (i have not learned anything about asynchronus programing therefore i would like to avoid it completely)请不要建议使用 HttpClient 之类的替代方法,因为我在其他地方也需要类似的代码,所以我只想要一个可以在同步应用程序中运行的代码(我还没有学到任何关于异步编程的知识,因此我想避免它完全地)

my approach was like suggested by Andrew Arnott in this thread Asynchronously wait for Task<T> to complete with timeout我的方法就像 Andrew Arnott 在这个线程中建议的那样异步等待 Task<T> 以超时完成

however my issue is, I am not exactly sure what type of variable "SomeOperationAsync()" is in his example (i mean it seems like a task, but how can i put actions into the task?), and the bigger issue is that VS wants to switch the complete Method to asynchronos, but i want to run everything synchronous but just with a timeout for a specific line of code.但是我的问题是,我不确定他的示例中的变量“SomeOperationAsync()”是什么类型的(我的意思是它看起来像是一项任务,但我怎样才能将操作放入任务中?),更大的问题是VS 想将完整的方法切换为异步,但我想同步运行所有内容,但只需要特定代码行的超时。

In case the question has been answered somewhere kindly provide a link如果问题已在某处得到解答,请提供链接

Thank you for any help!!感谢您的任何帮助!!

You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive and add using System.Reactive.Linq;您应该使用 Microsoft 的 Reactive Framework (aka Rx) - NuGet System.Reactiveusing System.Reactive.Linq; - then you can do this: - 然后你可以这样做:

var downloadString =
    Observable
        .Using(() => new WebClient(), wc => Observable.Start(() => wc.DownloadString("testurl")))
        .Select(x => new { success = true, result = x });

var timeout =
    Observable
        .Timer(TimeSpan.FromSeconds(5.0))
        .Select(x => new { success = false, result = (string)null });
        
var operation = Observable.Amb(downloadString, timeout);

var output = await operation;

if (output.success)
{
    Console.WriteLine(output.result);
}

The first observable downloads your string.第一个 observable 下载你的字符串。 The second sets up a timeout.第二个设置超时。 The third, uses the Amb operator to get the result from which ever of the two input observables completes first.第三,使用Amb运算符从两个输入 observable 中的哪一个最先完成获得结果。

Then we can await the third observable to get its value.然后我们可以await第三个 observable 来获取它的值。 And then it's a simple task to check what result you got.然后检查你得到的结果是一项简单的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM