Many custom DLL components are used in the scheduledjob project. To ensure the running efficiency, we use the cache method to load these components in advance, you can find and run the corresponding components in the memory when you wait for the application, instead of loading them every time. however, these components may change, add, or modify at any time. To ensure that the cached content is updated in a timely manner, you must adopt a mechanism to ensure that these component tables are more, can be updated to the memory cache at the same time.
In C #, we can use the filesystemwatcher component.
Using System; Using System. Collections. Generic; Using System. IO; Namespace Filesystemwatcherdemo { /// <Summary> /// Filesystemwatcher component application demo 1 /// </Summary> Class Filesystemwatcherdemo { Private Filesystemwatcher Dllwatcher; Static void Main ( String [] ARGs ){ Console . Writeline ( "File watcher started" + Datetime . Now. tostring ()); Filesystemwatcherdemo Watcher = New Filesystemwatcherdemo (); Watcher. beginwatcher (); Console . Read ();} Private void Beginwatcher () {dllwatcher = New Filesystemwatcher (); Dllwatcher. Path = @ "D: \ scheduledjob \ DLL" ; Dllwatcher. includesubdirectories = False ; Dllwatcher. Filter = "*.*" ; Dllwatcher. policyfilter = Notifyfilters . Lastwrite; dllwatcher. enableraisingevents = True ; Dllwatcher. Changed + = New Filesystemeventhandler (Dllwatcher_changed );} Void Dllwatcher_changed ( Object Sender, Filesystemeventargs E ){ Try { Console . Writeline ( "File" + E. fullpath + "Was updated" + Datetime . Now. tostring ());} Catch ( Exception Ex ){ Console . Writeline ( "File" + E. fullpath + "Can't be updated" + Datetime . Now. tostring () + ex. Message );}}}}
Test: after running, a 123.txt file is dropped in the dllfolder and the following result is displayed:
If a problem is found, a file is lost, and the onchange event is triggered multiple times (twice), a file is parsed and executed multiple times.
At present, no solution has been found for this situation. To ensure a change, the changed event is triggered only once, and only some measures that are not the solution can be taken to avoid it. Based on the features that these trigger events are completed in a short period of time, a key-value pair is used here, the key (File Name) value (the time point when the level is executed) to record the time of a file change. When it is triggered again, check the key-value pair, find the corresponding file, and check the time of its last execution, if it is within the specified time period, return and cancel this execution.
Using System; Using System. Collections. Generic;Using System. IO; Namespace Filesystemwatcherdemo { /// <Summary> /// Filesystemwatcher groupé T | used? Demo /// </Summary> Class Filesystemwatcherdemo { Private Filesystemwatcher Dllwatcher; // Multiple events used to resolve a change to the same file trigger question Private Dictionary < String ,Datetime > Watcherchangedtimes = Null ; Static void Main ( String [] ARGs ){ Console . Writeline ( "File watcher started" + Datetime . Now. tostring ()); Filesystemwatcherdemo Watcher = New Filesystemwatcherdemo (); Watcher. beginwatcher (); Console . Read ();} Private void Beginwatcher () {watcherchangedtimes = New Dictionary < String , Datetime > (); Dllwatcher = New Filesystemwatcher (); Dllwatcher. Path = @ "D: \ scheduledjob \ DLL" ; Dllwatcher. includesubdirectories = False ; Dllwatcher. Filter = "*.*" ; Dllwatcher. policyfilter =Notifyfilters . Lastwrite; dllwatcher. enableraisingevents = True ; Dllwatcher. Changed + = New Filesystemeventhandler (Dllwatcher_changed );} Void Dllwatcher_changed ( Object Sender, Filesystemeventargs E ){ # Region The same file is only processed once in 60 seconds.
Datetime Now = Datetime . Now;Int Reloadseconds = 60; If (Watcherchangedtimes. containskey (E. fullpath )){ If (Now. Subtract (watcherchangedtimes [E. fullpath]). totalseconds <reloadseconds ){ Return ;} Else {Watcherchangedtimes [E. fullpath] = now ;}} Else {Watcherchangedtimes. Add (E. fullpath, now );} # Endregion try {Console . Writeline ( "File" + E. fullpath + "Was updated" + Datetime . Now. tostring ());} Catch ( Exception Ex ){ Console . Writeline ( "File" + E. fullpath + "Can't be updated" + Datetime . Now. tostring () + ex. Message );}}}}
Then drop a file 1.jpg and the running result is as follows: