c# 在几秒钟后停止流读取器。这可能吗?
我有网络请求,我使用流阅读器读取信息。我想在 15 秒后停止在这个流读取器之后。因为有时阅读过程需要更多时间,但有时却进展顺利。如果阅读过程花费的时间超过 15 秒,我该如何停止它?我开放所有想法。
I have web request and i read information with streamreader. I want to stop after this streamreader after 15 seconds later. Because sometimes reading process takes more time but sometimes it goes well. If reading process takes time more then 15 seconds how can i stop it? I am open all ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 System.Threading.Timer 并设置一个 15 秒的滴答事件。它不是最干净的,但它会起作用。或者也许是秒表
--stopwatch 选项
--Timer 选项
Use a System.Threading.Timer and set an on tick event for 15 seconds. It's not the cleanest but it would work. or maybe a stopwatch
--stopwatch option
--Timer option
既然您说“网络请求”,我假设流读取器包装了您通过调用
httpWebRequest.GetResponse 从
。HttpWebRequest
实例获取的System.IO.Stream
().GetResponseStream()如果是这种情况,您应该查看 <代码>HttpWebRequest.ReadWriteTimeout。
Since you say "web request", I assume that the stream reader wraps a
System.IO.Stream
which you obtained from aHttpWebRequest
instance by callinghttpWebRequest.GetResponse().GetResponseStream()
.If that's the case, you should take a look at
HttpWebRequest.ReadWriteTimeout
.您必须在另一个线程中运行该任务,并从主线程监视它的运行时间是否超过 15 秒:
You will have to run the task in another thread, and monitor from your main thread whether it's running longer than 15 seconds: