紧凑框架套接字超时
我在检测 PDA 设备的 CF 应用程序中套接字连接丢失时遇到问题。 我有一个静态类,它具有用于通信的静态方法(Connect()、Write()、Disconnect())。静态是因为所有表单都可以调用 Write 方法。
在 Connect 方法中,我调用 socket.Connect(ipEndpoint); 但是当设备没有 wifi 连接时,程序会在这条线上暂停大约 20 秒,这太长了。此外,如果用户启动 Write() 方法(保存一些数据)并且 wifi 连接丢失,用户将无法与表单交互并认为应用程序出现问题。由于 CF 套接字连接没有超时选项,那么控制套接字行为的最佳方法是什么? 我的想法是当套接字 5 秒没有响应时显示某种“通信形式”,它将尝试重新建立连接。此表单将具有图形指示器(旋转时钟或类似的东西),以向用户显示程序正在尝试连接,并在用户决定退出应用程序时退出按钮。如果 socket.connect 成功,我将向用户显示上次使用的表单。 我认为这必须使用线程来完成,但因为我没有这方面的经验。我需要帮助如何管理这种行为。
I have problem detecting the loss of socket connection in CF app for PDA device.
I have static class that has static methods for communication (Connect(), Write(), Disconnect()). Static because all forms can call Write method.
In Connect method i call socket.Connect(ipEndpoint);
But when device hasn't got wifi connection program halts at this line for about 20 s which is too long. Also if user starts Write() method (saving some data) and wifi connection is lost, user cannot interact with form and thinks that application frizzed. Since there is no timeout option for CF socket connection, what is the best way to control socket behavior?
My idea is to show some kind of "Communication form" when socket doesn't response for 5 seconds which will try to reestablish connection. This form will have graphical indicator (rotating clock or something like that) to show user that program is trying to connect and exit button if user decides to exit app. If socket.connect succeeds, i will show last used form to user.
I assume that this has to be done with Threads, but since i don't have experience with it. i need help how to manage this behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以调用 Socket.BeginConnect() 在后台启动连接。然后,您可以指定当套接字已连接(或超时)时将调用的回调方法。此外,要实现进度条在尝试连接时倒计时,您可以执行以下操作:
然后您可以让连接表单使用计时器倒计时,通过调用检查连接的状态:
轮询效率不是很高,但在在这种情况下,它与您描述的弹出连接表单配合得很好。
You can call Socket.BeginConnect() to launch the connect in the background. You can then specify the callback method that will get invoked when the socket has connected (or timed-out). Additionally, to implement your progress bar counting down as it tries to connect you can do:
And then you can have your connection form use a timer to count down, checking the status of the connection by calling:
Polling is not very efficient, but in this case it works well with your described pop-up connection form.