如何在C#中建立长轮询客户端?(How to do a long polling client in C#?)

编程入门 行业动态 更新时间:2024-10-11 23:22:19
如何在C#中建立长轮询客户端?(How to do a long polling client in C#?)

我有一个C#桌面应用程序,我使用Web服务没有问题(wsdl添加“添加服务引用”,所以我创建一个对象并调用其功能)。

现在,我想使用长轮询技术,但我无法从客户端的角度来看如何做到这一点。

如何配置超时? 我应该使用专用于此的线程吗? C#桌面应用程序有什么示例吗? (还没找到)

谢谢,大坝的

I have a C# desktop application, and I consume a web service without problems (wsdl added by "add Service References", so I create an object and call its functions).

Now, I want to use long polling techniques but I can't figure how to do this from the client's perspective.

How to configure the timeout ? Should I use a thread dedicated to this ? Is there any example for a C# desktop application ? (haven't found any)

Thanks, Dam's

最满意答案

您应该能够在Web服务对象上配置超时 - 详细信息将取决于它正在使用的确切类,但请查看WebClientProtocol.Timeout以获取示例。

现在,您可以从专用线程同步调用它,或者您可以对Web服务进行异步调用,指定在服务回复时要执行的回调(可能在线程池线程上)。 在这种情况下,您可能会发现可以在异步调用本身上指定超时 - 再次,它将取决于您所拥有的Web服务代理类的确切类型。

这样你就不需要“浪费”一个等待响应的线程 - 但你可能会发现异步编程模型比同步编程模型更难理解。 如果您在任何时候只收到其中一个或两个请求,则额外的几个线程不太可能成为问题。 如果您正在等待来自500种不同服务的响应,那么这是另一回事,异步模型肯定是要走的路。

You should be able to configure the timeout on the web service object - the details will depend on exactly which class it's using, but look at WebClientProtocol.Timeout for an example.

Now you could either call that synchronously from a dedicated thread, or you could make an asynchronous call to the web service to start with, specifying a callback to be executed (probably on a thread pool thread) when the service replies. In that case, you may find you can specify the timeout on the asynchronous call itself - again, it will depend on exactly what kind of web service proxy class you've got.

That way you don't need to "waste" a thread just waiting for the response - but you may find that the asynchronous programming model is harder to understand than the synchronous one. If you've only got one or two of these requests at any one time, the extra couple of threads is unlikely to be an issue. If you're waiting for responses from 500 different services, that's a different matter and the async model would definitely be the way to go.

更多推荐

本文发布于:2023-07-18 04:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   如何在   长轮询   client   long

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!