问题立即停止streamreader [英] Problem stopping streamreader right away

查看:123
本文介绍了问题立即停止streamreader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用一个特殊的内部网站在内部设备上执行ping操作.这就是为什么我要使用网站来运行ping而不是仅仅这样做更简单的原因.
问题:我在停止从网站上读取信息流的流读取器时遇到问题.问题是,如果我尝试停止阅读器,它实际上不会停止,直到它从流中接收到更多信息为止.流读取器也处于线程中.我缺少明显的东西来立即停止流阅读器吗?

Background: I am using a special internal website to run pings on internal devices. That is why i am using a website to run the pings instead of just doing it an easier way.

Problem: I am having a problem stopping a streamreader that is reading information streaming from a website. The problem is if i try to stop the reader it won''t actually stop until it receives more information from the stream. The streamreader is in a thread as well. Am i missing something obvious to stop the streamreader immediately?

Private Sub DoThreadedPing()
Dim client As New WebClient
Dim strm As Stream
Dim sr As StreamReader
Dim pingTXT As String
Try
    strm = client.OpenRead(New Uri(Ping1Info))
    sr = New StreamReader(strm)
    Do
         pingTXT = sr.ReadLine
         If pingTXT IsNot Nothing Then TextBox1.Invoke(New PingDelegate(AddressOf WritePing), pingTXT)
    Loop While pingTXT IsNot Nothing

    Catch ex As Exception
...

    Finally
...
End Sub



*更新1 *
问题似乎出在"pingTXT = sr.ReadLine"代码中.使用BackgroundWorker进行了尝试,效果更好一些,但仍要等到接收到数据后才能取消该工作器.

是否有更好的方法在使用ReadLine命令之前检查数据,以便仅在有数据时尝试读取数据,而不尝试读取并等待数据到达?

*更新2 *
现在使用一系列BackgroundWorker使其工作,并且仅跟踪哪些仍在忙,并移至下一个Worker,直到我发现一个不忙并对该主机执行ping操作.无法使BeginRead正常工作,我可能做错了.



*update 1*
The problem seems to lay in the "pingTXT = sr.ReadLine" code. Tried using a BackgroundWorker and it works a little better but still waits until it receives data before cancelling the worker.

Is there a better way to check for data before using the ReadLine command so it only tries to read when there is data and not try to read and wait for data to arrive?

*update 2*
Got it working now using an array of BackgroundWorker''s and just tracking which ones are still busy and move to the next Worker until i find one not busy and run the ping on that one. Couldn''t get the BeginRead thing to work, i was probably doing something wrong.

推荐答案

使用BackgroundWorker对象而不是普通线程.它们很容易使用,因为您可以调用BackgroundWorker.CancelAsync()并完成它.
Use a BackgroundWorker object instead of a normal thread. They are much easier to work with because you can just call BackgroundWorker.CancelAsync() and be done with it.


您可以异步读取流,即strm.BeginRead参见此处查看一下 [
You can Asynchronously read the stream, i.e. strm.BeginRead see link[^] for documentation. You could also take a look here[^] for an article on an asynchronous streamreader.


这只是另一个认为:

使用另一个模块级别的volatile变量(布尔变量将用于此目的),它表示线程的状态.而不是完全中止线程,您将更改变量的状态.执行必须走出循环,最后将被执行.
This is just another thought:

Use another module level volatile variable(a boolean variable will serve the purpose) which will denote the status of the thread. Instead of aborting the thread completely you will change the status of variable. The execution has to come out of the loop and finally will be executed.


这篇关于问题立即停止streamreader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆