简体   繁体   English

C#线程和轮询

[英]C# threading and polling

I have a tray icon that needs to display two icons: 我有一个托盘图标,需要显示两个图标:

  1. If there is network connectivity, display a green circle with a check mark 如果存在网络连接,请显示带有复选标记的绿色圆圈
  2. If there isn't network connectivity, display a red circle with an X 如果没有网络连接,请显示带有X的红色圆圈

So what I have is: 所以我有:

using System.Net.NetworkInformation;

bool isConnected = NetworkInterface.GetIsNetworkAvailable()

So I'm thinking of starting a new thread or using the background worker progress because the tray icon is a NotifyIcon which is a component so I can't use: 所以我在考虑启动一个新线程或使用后台工作进程,因为任务栏图标是一个NotifyIcon,它是一个组件,因此我不能使用:

Form.Invoke(delegate, object[])

to update the icon property of the NotifyIcon class. 更新NotifyIcon类的icon属性。

My big concern is the polling process: I could write some logic that does: 我最关心的是轮询过程:我可以写一些能做到的逻辑:

while (true) 
{
    System.Threading.Thread.Sleep(1000);
    isConnected = NetworkInterface.GetIsNetworkAvailable();
    if (isConnected)
        notifyIcon.Icon = "ConnectedIcon.ico";
    else
        notifyIcon.Icon = "DisconnectedIcon.ico";
}

but I've seen a couple of articles that tell me to stay away from Sleep(1000). 但是我看过几篇文章,告诉我不要使用Sleep(1000)。 I can't seem to find those articles since I didn't bookmark them. 我似乎找不到这些文章,因为我没有将它们添加为书签。 I'm just curious to know why that isn't a good idea for polling in a thread. 我只是想知道为什么这不是在线程中轮询的好主意。

You can register an Event on NetworkChange so you are being notified when the status changes: 您可以在NetworkChange上注册一个事件,以便在状态更改时收到通知:

NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)  
{
    if (e.IsAvailable) {
        Console.WriteLine("Network Available");
    } else {
        Console.WriteLine("Network Unavailable");
    }
}

In you situation its totally fine to use the Sleep method. 在您的情况下,使用Sleep方法完全可以。

What you saw was probably saying so its better to use a Reset Event - from looping etc... 您所看到的可能是在说,因此最好使用Reset事件 -从循环等开始……

Polling isn't always evil, but it's best avoided if possible. 轮询并不总是邪恶的,但如果可能的话,最好避免。 If I run your application that's polling once per second, that means that once per second your program is getting scheduled to do work on a CPU core that is 99.9999% going to be a no-op. 如果我运行的应用程序每秒轮询一次,则意味着您的程序将按计划每秒运行一次,CPU内核的工作率为99.9999%,这是无操作的。 On a desktop that's not too terrible, but imagine a laptop. 在不太可怕的台式机上,但是想像一台笔记本电脑。 CPUs there try run in very low power modes whenever possible, so additional CPU work means less battery life! 那里的CPU尽可能尝试在低功耗模式下运行,因此额外的CPU工作量意味着电池寿命缩短! This is the reason why many mobile platforms (iOS, Windows Phone 7, etc) ban arbitrary background threads because they know people will abuse them. 这就是许多移动平台(iOS,Windows Phone 7等)禁止任意后台线程的原因,因为它们知道人们会滥用它们。

In your case, there's an easier way: just use System.Net.NetworkInformation.NetworkChange which provides events for when the network connectivity changes. 在您的情况下,有一种更简单的方法:只需使用System.Net.NetworkInformation.NetworkChange提供有关网络连接更改的事件。 No polling required! 无需轮询!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM