如何同步2 FileSystemWatcher的输出

时间:2011-12-15 07:57:12

标签: c# multithreading filesystemwatcher

我已经实例化了2个FileSystemWatcher,我现在想要的是同步两个filesystemwatcher。截至目前,此处是日志的示例输出:(注意格式的更改是指其他FSM,反之亦然)

Dec 15 15:28:19,8167876|: INFO   |Web server call start_device_discovery() for: 10.191.20.0
Dec 15 15:28:19,8167876|: START  |      Device discovery started for: 10.191.20.0
Dec 15 15:28:19,9261472|: INFO   |Discovery is active for: NET_10.191.20.0_24
Dec 15 15:28:20,1136208|: INFO   |Device discovered from NET_10.191.20.0_24 BaseModelName: "TASKalfa 250ci", ModelName: "TASKalfa 250ci", MoName: "TASKalfa 250ci"
Dec 15 15:28:20,3948312|: INFO   |Device discovered from NET_10.191.20.0_24 BaseModelName: "FS-C2626MFP", ModelName: "FS-C2626MFP", MoName: "FS-C2626MFP"
Dec 15 15:28:20,4260768|: INFO   |Device discovered from NET_10.191.20.0_24 BaseModelName: "FS-3640MFP", ModelName: "FS-3640MFP", MoName: "FS-3640MFP"
Dec 15 15:28:20,5198136|: INFO   |Device discovered from NET_10.191.20.0_24 BaseModelName: "TASKalfa 7550ci", ModelName: "TASKalfa 7550ci", MoName: "TASKalfa 7550ci"
Dec 15 15:28:20,6135504|: INFO   |Device discovered from NET_10.191.20.0_24 BaseModelName: "FS-C2626MFP", ModelName: "FS-C2626MFP", MoName: "FS-C2626MFP"
2011-12-15 15:28:19,785|  INFO | NetworkList - setStartDiscovery - Starting point
2011-12-15 15:28:19,785|  INFO | NetActionProvider - performAction - JYR
2011-12-15 15:28:19,785|  INFO | NetActionProvider - ATN_DISCOVERY_START
2011-12-15 15:28:19,785|  INFO | SNMPDiscovery - Starting discovery for NET_10.191.20.0_24.

请仔细看看他们的时间。 15:28:19,8167876ms的声明在15:28:19,785ms之前首次输出

Dec 15 15:28:19,8167876|: INFO   |Web server call start_device_discovery() for: 10.191.20.0
2011-12-15 15:28:19,785|  INFO | NetworkList - setStartDiscovery - Starting point

我想做的是尝试同步这两位观察者。有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试同步日志访问。为此,您需要一个简单的对象进行同步,所有FileSystemWatcher事件处理程序都可以访问该对象。为简化起见,您可以使用logger对象(但请阅读remarks section here)。 FileSystemWatcher获得通知后立即获取锁

private static void OnChanged(object source, FileSystemEventArgs e)
{
    lock(MyLogger)
    {
        MyLogger.Log("something changed");
    }
}

确保在使用记录器的任何地方锁定。