简体   繁体   English

从telnet读取backgroundworker循环-C#

[英]backgroundworker loop to read from telnet - C#

Fairly new to programming but need this code to get some hardware I am building working. 对编程来说还算是新手,但是需要此代码来获得我正在构建的硬件。

I am writing ac# program to connect to a telnet server and read from the feed then display in a text box (sounds simple enough)... 我正在编写ac#程序以连接到telnet服务器并从提要中读取内容,然后显示在文本框中(听起来很简单)...

A connect button connects the telnet and runs a backgroundworker that then waits for the telnet session to send data and then outputs to the text box in the complete section of the background worker. 连接按钮将连接telnet并运行backgroundworker,然后它会等待telnet会话发送数据,然后输出到background worker完整部分中的文本框。

My question is, how then do I get the background worker to run again once that read is received? 我的问题是,一旦收到读取,我该如何让后台工作者再次运行?

If i use a whil loop in the connect button that encapsulates the backgroundworker starter then the ui freezes up (which was the whole point of using a background worker to start with). 如果我在封装backgroundworker启动程序的连接按钮中使用whil循环,则ui冻结(这是使用background worker的全部重点)。

THanks for your help. 谢谢你的帮助。

Shags 粗毛

You don't say how you're reading from the telnet server. 您无需说说自己是如何从telnet服务器读取信息的。

I wouldn't recommend using a BackgroundWorker or even a Task for this. 我不建议为此使用BackgroundWorkerTask You can have your GUI code create a TcpClient instance. 您可以让您的GUI代码创建一个TcpClient实例。 Once that's created, the button handler calls BeginConnect , passing a reference to the function that will be called when the connection is made. 创建完之后,按钮处理程序将调用BeginConnect ,并将引用传递给建立连接时将要调用的函数。

When the connection callback is called, it gets the stream (calls GetStream ), and calls BeginRead on the stream. 调用连接回调时,它将获取流(调用GetStream ),并在流上调用BeginRead And the read handler calls BeginRead again. 读取处理程序再次调用BeginRead So what you end up with is an asynchronous loop. 所以最终得到的是一个异步循环。

See the examples for BeginConnect and EndConnect , and the NetworkStream BeginRead and EndRead methods. 请参见BeginConnectEndConnect的示例以及NetworkStream BeginReadEndRead方法。

Also, you might get some good information from a search for [TcpClient asynchronous example]. 另外,通过搜索[TcpClient异步示例],您可能会获得一些有用的信息。

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

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