C#套接字没有定时器或while循环接收 [英] C# socket receive without timer or while loop

查看:96
本文介绍了C#套接字没有定时器或while循环接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我是c#中socket编程的新手。目前我正在开发Windows应用程序套接字。即我有一个c#windows应用程序,其中我想读取数据来每隔100毫秒插入插座

并在文本框中显示该数据。



我已经使用socket.receive方法通过使用计时器轮询来实现它。但是通过在Windows应用程序中使用计时器,它会挂起一段时间或只是数据延迟。



我想知道c#中有任何方法我可以当新数据到达时读取套接字.plz建议用一些例子。

Hi all
I am very newbie to socket programming in c#. Currently i am working on Windows Application socket receive. i.e I havea c# windows application in which i want to read data comming to socket
every 100ms and display that data in text box.

I had implemented that using socket.receive method by polling it using a timer. But by using a timer in windows Application it hangs some time or just data comes delayed.

I want to know that is there any method in c# by which i can just read the socket when new data arrives.plz suggest with some example.

推荐答案

Socket类不提供事件(不幸的是),但它确实允许你设置委托异步处理的处理程序。有一个BeginReceive方法,它允许您传入一个委托方法,以便在操作完成时调用。这些回调是您使用而不是处理OnReceive事件。

请参阅: MSDN Socket.BeginRecieve [ ^ ]并尝试一下。它比事件处理更笨拙,但它以类似的方式工作。
The Socket class does not provide Events (unfortunately), but it does allow you to set delegate handlers for asynchronous handling. There''s a BeginReceive method which let you pass in a delegate method to be called when the operation completes. These callbacks are what you use instead of handling OnReceive events.
See here: MSDN Socket.BeginRecieve[^] and give it a try. It''s rather more clumsy than event handling, but it works in a similar fashion.


这篇关于C#套接字没有定时器或while循环接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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