如何确定重新配置的网络适配器是否准备好传输?
我在以下过程中遇到问题:
- 我的应用程序调用以管理员身份运行的 Windows 服务来根据需要设置网络适配器的 IP 配置。
- 由于 Windows 服务无法轻松发出其已完成任务的信号,因此应用程序会监视网络适配器,直到出现所需的配置(有超时)。
- 应用程序通过 TCP 连接到目标 IP 地址。
我的问题是配置网络适配器(步骤 2)和可以使用网络适配器(步骤 3)之间存在延迟。如果我在重新配置适配器后尝试立即 ping 或通过 TCP 连接,则会收到错误:
无法到达目标主机。
如果我添加任意 5 秒的延迟,它可以正常工作,但这是一个糟糕的解决方案。我正在使用 Win32_NetworkAdapterConfiguration< /a> 用于重新配置和监视网络适配器的 WMI 类。我该怎么做才能确保适配器准备好传输而不会有竞争条件的风险?
I have a problem with the following process:
- My application calls a Windows Service running as Administrator to set the network adapter's IP configuration as required.
- Since a Windows Service cannot easily signal that it has completed the task, the application watches the network adapter until the required configuration is present (with a timeout).
- The application connects to the target IP address by TCP.
My problem is there is a delay between when the network adapter is configured (step 2) and when it can be used (step 3.) If I try to ping or connect via TCP immediately after reconfiguring the adapter, I get the error:
Destination host cannot be reached.
If I include an arbitrary 5-second delay, it works fine, but this is a terrible solution. I'm using the Win32_NetworkAdapterConfiguration WMI class to reconfigure and watch the network adapter. What can I do to ensure the adapter is ready to transmit without risking a race condition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论