是什么导致我的程序在 Windows 启动时出现延迟?
我的程序随 Windows 启动而启动,
但后台工作者应该在程序打开后立即工作。
但它一开始有延迟,甚至返回错误信号(如果站点启动则返回),
仅在大约 15 秒后,后台工作人员和程序就继续正常工作。我认为这是因为 .net 框架尝试加载,或者互联网连接尚未启动,或者尚未加载的内容(Windows 启动)。
什么可以解决这个问题,可能的原因是什么? (WinForm C#)
编辑:
这是我想到的,
但我不认为这是一个好的做法。有更好的办法吗?
(加载方法):
while (!netConnection())
{
}
if(netConnection())
bwCheck.RunWorkerAsync();
My program starts with windows startup,
But a background worker is supposed to work instantly after the program is opened.
But it starts with a delay and then even returns false signs(it returns if a site is up),
Only after about 15 seconds the background-worker continues to work normally and the program too. I think this is because of .net framework trying to load, or internet connection that is not up yet, or something that didn't load yet(windows startup).
What can solve this, and what is the probable cause? (WinForm C#)
Edit:
Here is something I thought of,
I don't think though that this is a good practice. Is there a better way?
(Load method):
while (!netConnection())
{
}
if(netConnection())
bwCheck.RunWorkerAsync();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Nope。如果是这种情况,你的程序将无法运行。
是的。网卡/接口/连接/任何尚未初始化并连接到互联网的东西。您不能期望 PC 在启动时立即连接到互联网。更重要的是,如果您的客户位于使用网络身份验证的域中怎么办?如果他们延迟网络通信直到某些任务完成(这实际上是我下面的案例中的问题。说真的。)
在这种情况下,可能需要更长的时间才能启动并运行它(阅读:不要添加
Thread.Sleep()
徒劳地尝试“解决”这个问题,我不得不在系统设计中解决这样的问题,我们通过 PC 中的以太网总线与运动控制板进行通信。最后添加了一些代码来监视状态的网络连接,只有当它建立时,才开始通过网卡与设备通信编辑:
正如 SLaks 在评论中指出的那样,这在 C# 中非常简单:NetworkAvailabilityChanged 事件 为您带来编程乐趣。
Nope. If that were the case your program wouldn't run.
Yup. The network card/interface/connection/whatever is not initialized and connected to the internet yet. You can't expect a PC to be connected to the internet immediately at startup. Even more, what if your customer is on a domain using network authentication? What if they delay network communications until some task is complete (this was actually the problem in my case below. Seriously.)
It may take even longer to get it up and running in that case (read: don't add a
Thread.Sleep()
in a vain attempt to 'fix' the issue.I had to fix a problem like this once in a systems design where we communicated to a motion control board via the ethernet bus in a PC. I ended up adding some code to monitor the status of the network connection and, only when it was established, started talking to the device via the network card.
EDIT: As SLaks pointed out in the comments, this is pretty simple in C#: The NetworkAvailabilityChanged event for your programming pleasure.
这绝对是因为一切都还在启动。在您登录后很长时间内,服务仍然可以在线,您看到的快速登录对话框是 Windows 中的一项优化,可以让您在其他一切仍然启动的情况下登录。
请注意
如何在 C# 中检测工作的互联网连接?
特别是一种避免环回适配器的技术:
It is absolutely because of everything still starting up. Services can still be coming online long after you log in, the quick login dialog you see was an optimization in windows to let you log in while everything else still starts up.
Take note of
How to detect working internet connection in C#?
specifically a technique that avoids the loopback adapter: