【问题标题】:EnableRaisingEvents every 100 milliseconds?EnableRaisingEvents 每 100 毫秒?
【发布时间】:2013-03-05 17:44:10
【问题描述】:

我只是想知道是否有每 100 毫秒打开和关闭 EnableRaisingEvents 的方法? 例如,最初,EnableRaisingEvents 设置为 true。当有事件进入时,EnableRaisingEvents 设置为 false。 100 毫秒后,EnableRaisingEvents 再次设置为 true。在文件观察者收到另一个事件之前,EnableRaisingEvents 再次设置为 false。 在 EnableRaisingEvents 关闭(设置为 false)期间错过事件是可以的。 所以我只是想知道我应该如何在代码中实现这个逻辑。让我感到困惑的是,无论如何该事件都会被触发,我是否应该使用勾号来决定是否接受这个事件?

【问题讨论】:

  • 不清楚您要解决什么问题。请记住,将 EnableRaisingEvents 设置回 false 意味着“停止引发事件”。在那之后的一段时间内,事件可能会被触发。并且您最好不要忘记订阅 Error 事件。
  • 我想我提到了“在 EnableRaisingEvents 关闭期间错过事件是可以的(设置为 false)”。

标签: c# events filesystemwatcher


【解决方案1】:

您可以尝试这样的事情(根据 Austin Salonen 的建议进行编辑):

private static Timer _myTimer = new Timer();
// ...
private static void OnTick(Object obj, EventArgs args)
{
    fileSystemWatcher.EnableRaisingEvents ^= true;
}

// ...

_myTimer.Tick += OnTick;
_myTimer.Interval = 100;
_myTimer.Start();

【讨论】:

  • 为什么不在 OnTick 中切换 EnableRaisingEvents 而不是共享布尔值?
  • @AustinSalonen 我不明白你的意思。你能稍微澄清一下吗?
  • _raiseEvent代替fsw.EnableRaisingEvents ^= true;
  • 我想我错过了什么,fsw.EnableRaisingEvents 到底是什么?
  • FileSystemWatcher.EnableRaisingEvents
【解决方案2】:

使用 Timer(线程一)并将经过的时间间隔设置为 100 ms,如果在 UI 线程上创建 FS wather,您可能需要序列化调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多