2024年3月13日发(作者:)

解决方案代码

[csharp]viewplaincopyprint?

ystem;

c;

;

;

;

ing;

7.

aceShareReadFile

9.{

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

publicclassDirectoryMonitor:IDirectoryMonitor

{

privatereadonlyFileSystemWatcherm_fileSystemWatcher=newFile

SystemWatcher();

ereadonlyDictionarym_pendingEvents=n

ewDictionary();

22.

23.

24.

25.

26.

27.

28.

29.

hange);

publicDirectoryMonitor(stringdirPath)

{

m_=dirPath;

m_eSubdirectories=false;

m_d+=newFileSystemEventHandler(OnC

privatereadonlyTimerm_timer;

privateboolm_timerStarted=false;

}

publicinterfaceIDirectoryMonitor

{

eventFileSystemEventChange;

voidStart();

publicdelegatevoidFileSystemEvent(Stringpath);

30.

hange);

31.

32.

te);

33.

34.

35.

36.

37.

38.

39.

40.

41.

42.

43.

44.

ightnow

45.

46.

47.

th

48.

49.

50.

51.

52.

53.

54.

55.

56.

57.

58.

59.

60.

}

}

}

m_d+=newFileSystemEventHandler(OnC

m_timer=newTimer(OnTimeout,null,te,Timeou

publiceventFileSystemEventChange;

publicvoidStart()

{

m_RaisingEvents=true;

privatevoidOnChange(objectsender,FileSystemEventArgse)

{

//Don'twantotherthreadsmessingwiththependingeventsr

lock(m_pendingEvents)

{

//Saveatimestampforthemostrecenteventforthispa

m_pendingEvents[th]=;

//Startatimerifnotalreadystarted

if(!m_timerStarted)

{

m_(100,100);

m_timerStarted=true;

}

}

privatevoidOnTimeout(objectstate)

{

61.

62.

63.

ightnow

64.

65.

66.

Listpaths;

//Don'twantotherthreadsmessingwiththependingeventsr

lock(m_pendingEvents)

{

//Getalistofallpathsthatshouldhaveeventsthrown

67.

68.

69.

70.

71.

72.

73.

74.

75.

76.

77.

78.

79.

80.

81.

82.

83.

84.

85.

86.

87.

88.

89.

90.

events)

91.

92.

{

}

});

}

paths=FindReadyPaths(m_pendingEvents);

//Removepathsthataregoingtobeusednow

h(delegate(stringpath)

{

m_(path);

});

//Stopthetimeriftherearenomoreeventspending

if(m_==0)

{

m_(te,te);

m_timerStarted=false;

}

//Fireaneventforeachpaththathaschanged

h(delegate(stringpath)

{

FireEvent(path);

privateListFindReadyPaths(Dictionary

Listresults=newList();

93.

94.

95.

96.

97.

5ms

98.

99.

DateTimenow=;

foreach(KeyValuePairentryinevents)

{

//Ifthepathhasnotreceivedaneweventinthelast7

//aneventforthepathshouldbefired

doublediff=ct().TotalMilliseconds;

100.

101.

102.

103.

104.

105.

106.

107.

108.

109.

110.

111.

112.

113.

114.

115.

116.

117.

118.}

}

}

}

}

}

if(diff>=75)

{

();

}

returnresults;

privatevoidFireEvent(stringpath)

{

FileSystemEventevt=Change;

if(evt!=null)

{

evt(path);