我必须创建一个Windows服务,它监视指定文件夹中的新文件并对其进行处理并将其移动到其他位置.
我开始使用FileSystemWatcher
.我的老板不喜欢FileSystemWatcher
并希望我使用Timer
除了之外的任何其他机制来使用轮询FileSystemWatcher
.
如何在不FileSystemWatcher
使用.NET框架的情况下监控目录?
Mik*_*son 17
实际上,根据我多年来的经验,FileWatcher组件并非100%"稳定".将足够多的文件压入文件夹,您将丢失一些事件.如果监视文件共享,即使增加缓冲区大小,也尤其如此.
因此,出于所有实际原因,请使用FileWatcher和Timer扫描文件夹中的更改,以获得最佳解决方案.
如果你谷歌它,创建计时器代码的例子应该是丰富的.如果在计时器运行时跟踪最后一个DateTime,则检查每个文件的修改日期,并将其与日期进行比较.相当简单的逻辑.
计时器间隔取决于系统更改的紧急程度.但是检查每一分钟应该适用于许多场景.
使用@ Petoj的答案我已经包含了一个完整的Windows服务,每五分钟轮询一次新文件.它只限于一个线程轮询,占用处理时间并支持暂停和及时停止.它还支持在system.start上轻松附加debbugger
public partial class Service : ServiceBase{
List<string> fileList = new List<string>();
System.Timers.Timer timer;
public Service()
{
timer = new System.Timers.Timer();
//When autoreset is True there are reentrancy problems.
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
}
private void DoStuff(object sender, System.Timers.ElapsedEventArgs e)
{
LastChecked = DateTime.Now;
string[] files = System.IO.Directory.GetFiles("c:\\", "*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
if (!fileList.Contains(file))
{
fileList.Add(file);
do_some_processing();
}
}
TimeSpan ts = DateTime.Now.Subtract(LastChecked);
TimeSpan MaxWaitTime = TimeSpan.FromMinutes(5);
if (MaxWaitTime.Subtract(ts).CompareTo(TimeSpan.Zero) > -1)
timer.Interval = MaxWaitTime.Subtract(ts).TotalMilliseconds;
else
timer.Interval = 1;
timer.Start();
}
protected override void OnPause()
{
base.OnPause();
this.timer.Stop();
}
protected override void OnContinue()
{
base.OnContinue();
this.timer.Interval = 1;
this.timer.Start();
}
protected override void OnStop()
{
base.OnStop();
this.timer.Stop();
}
protected override void OnStart(string[] args)
{
foreach (string arg in args)
{
if (arg == "DEBUG_SERVICE")
DebugMode();
}
#if DEBUG
DebugMode();
#endif
timer.Interval = 1;
timer.Start();
}
private static void DebugMode()
{
Debugger.Break();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36749 次 |
最近记录: |