在 C# 中创建空闲循环的好方法?
我有一个应用程序,它设置了一个 FileSystemWatcher。它应该无限期地运行。
让它在空闲循环中运行的最佳方法是什么?
我目前正在做的
FileSystemWatcher watch = ... //setup the watcher
watch.EnableRaisingEvents = true;
while (true)
{
Thread.Sleep(int.MaxValue);
}
事情似乎可行(即捕获事件并且不会在繁忙的循环中耗尽核心)。
还有什么成语吗?这种方法有什么问题吗?
I have an app it sets up a FileSystemWatcher. It should run indefinitely.
What is the best way to have it run in an idle loop?
I'm currently doing
FileSystemWatcher watch = ... //setup the watcher
watch.EnableRaisingEvents = true;
while (true)
{
Thread.Sleep(int.MaxValue);
}
which seems to work (ie, captures the events and doesn't use up a core in a busy loop).
Any other idiom? Any problem with this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
FileSystemWatcher.WaitForChanged 是阻塞(同步)。 绝对不需要空闲循环,特别是因为您正在处理事件。注意 EnableRaisingEvents 默认为 true。因此,您所要做的就是将组件添加到表单中。没有空闲循环。没有线程,因为组件负责处理它。
我了解到您正在使用控制台应用程序。因此,您可以创建以下循环。同样,不需要空闲循环。该组件负责处理所有细节。
[更新]“注意 EnableRaisingEvents 默认为 true。”根据 Microsoft 源代码,只有将 FileSystemWatcher 组件拖放到设计器上时才会出现这种情况。如下所示:
[更新] 在
System.Threading.Monitor.Wait
语句之前和之后调用WaitForChanged
会设置 EnableRaisingEvents。FileSystemWatcher.WaitForChanged is blocking (synchronous). There is absolutely no need for an idle loop, especially because you are handling events. Note EnableRaisingEvents defaults to true. Therefore, all you have to do is add the component to a form. There is no idle loop. There is no threading because the component takes care of that.
I understand you are using a Console Application. Therefore, you can create the following loop. Again, no need for an idle loop. The component takes care of all the details.
[update] "Note EnableRaisingEvents defaults to true." As per the Microsoft source code, this is only true if the FileSystemWatcher component is dropped onto a designer. As shown below:
[update] Calling
WaitForChanged
sets EnableRaisingEvents immediately before and after theSystem.Threading.Monitor.Wait
statements.正如我从您的评论中了解到的,您需要您的应用程序无限期地运行,并且它是一个由另一个可执行文件提供的控制台应用程序。
最好使用 Console.ReadLine 而不是循环。
或使用 Monitor.Wait 方法无限期地阻塞线程
As I understood from your comment you need your application to run indefinitely and it is a console app that is lunched by another executable.
It is better to use Console.ReadLine instead of looping.
or use Monitor.Wait method to block the thread indefinitely
Thread.Sleep() 会做你想做的事。但基本上,您应该阻止使用会根据您想要的策略唤醒的操作系统方法。例如。 Console.ReadLine() 或互斥体上的互斥体,该互斥体被事件处理程序击中而消失。
您不必杀死您的应用程序才能将其关闭。
如果您将代码嵌入到表单中,那么至少可以很好地终止该表单,并且在您不使用它时具有合理的操作系统负载。
Thread.Sleep() will do what you want. But basically you should block using an OS method that will wake upon your desired strategy. Eg. a Console.ReadLine() or upon a mutex which is hit by your event handler to go away.
You shouldn't have to kill your app to make it shut.
If you embed the code on a form then at least the form can be killed nicely, with a reasonable OS load when you aren't using it.
如果将此循环运行的线程标记为后台线程 (Thread.IsBackground 属性)那么它应该可以正常工作(当所有非后台线程终止时程序将退出)
If you mark the thread where this loop is running as a background thread (Thread.IsBackground property) then it should work fine (the program will exit when all non background threads terminate)
我不明白 ...
为什么你想要空闲循环?您想让您的应用程序无限期地运行吗?
那么,为什么不创建一个Windows服务,或者一个可以在系统托盘中运行的应用程序呢?
然后,我要做的就是创建一个类来执行以下操作:
I do not understand ...
Why do you want and idle loop ? Do you want to have your application run indefinitely ?
Then, why don't you create a Windows Service, or an application that can be run in the system tray ?
Then, what I would do is create a class which just does this: