在C#中实现以下长时间运行线程的更好方法

问题描述 投票:0回答:2

我有一个应用程序,它有一个等待事件的主线程(例如,在目录中接收文件)并将该文件发布到API。这部分工作正常。

另一方面,由于某些原因,主线程可能会遗漏一些文件。所以,我已经设置了一个后台线程,可以在整个应用程序生命周期内运行,清理那些丢失的文件

public virtual void MissedFileHandler()
{
     if (_missedFileHandlerThread == null || !_missedFileHandlerThread.IsAlive)
     {
          _missedFileHandlerThread = new Thread(new ThreadStart(ClearMissedFiles));
          _missedFileHandlerThread.IsBackground = true;
          _missedFileHandlerThread.Start();
     }
}

protected virtual void ClearMissedFiles()
{
     while (true)
     {
         string[] missedFiles = new string[] { };
         missedFiles = Directory.GetFiles(ConfigurationHelper.applicationRootDirectory, "*.txt", SearchOption.TopDirectoryOnly);
         Parallel.ForEach(missedFiless, (currentMissedFile) =>
         {
             bool isFileInUse = true;
             isFileInUse = FileHelper.CheckIfFileInUse(filesInUseCollection, currentMissedFile)
             if (!isFileInUse)
             {
                 bool isHttpTransferSuccess = false;
                 isHttpTransferSuccess = FileHelper.SendFileToApi(userid, currentMissedFile);
             if (isHttpTransferSuccess)
             {
                  File.Delete(currentMissedFile);

             }
         }
     });
     Thread.Sleep(1000);
     }
  }

请注意,由于某些其他原因,不能将此作为Windows服务或调度程序运行。所以,我确实需要一个后台工作者来定期清理丢失的文件。我检查了TimerWaitHandles,但不太确定如何使用这些来实现以下内容。

这两个进程的方式(主要的甚至是新文件的处理程序和后台清理程序被调用,在主应用程序线程中我将实例化类并在应用程序启动时调用方法。这是Thread.Sleep()while (true)困扰我和我寻找更好的方法。谢谢。

c# multithreading while-loop
2个回答
3
投票

您可以使用文件监视器来检测该目录中的新文件。

private void watch()
{
  FileSystemWatcher watcher = new FileSystemWatcher();

  // Set your path with this
  watcher.Path = path;

  // Subscribe to event
  watcher.Changed += new FileSystemEventHandler(OnChanged);
  watcher.EnableRaisingEvents = true;
}

1
投票

尝试使用Microsoft的Reactive Framework。然后你可以这样做:

IObservable<Unit> fileChanges =
    Observable
        .Using(() =>
        {
            var fsw = new FileSystemWatcher();
            fsw.Path = path;
            fsw.EnableRaisingEvents = true;
            return fsw;
        }, fsw =>
            Observable
                .FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
                    h => fsw.Changed += h,
                    h => fsw.Changed -= h))
        .Select(ep => Unit.Default);

IObservable<Unit> periodically =
    Observable
        .Interval(TimeSpan.FromSeconds(30.0))
        .Select(x => Unit.Default);

IObservable<string> clearMissedFiles =
    fileChanges
        .Merge(periodically)
        .SelectMany(u => Directory.GetFiles(ConfigurationHelper.applicationRootDirectory, "*.txt", SearchOption.TopDirectoryOnly))
        .Where(f => !FileHelper.CheckIfFileInUse(filesInUseCollection, f))
        .SelectMany(f => Observable.Start(() => FileHelper.SendFileToApi(userid, f)), (f, s) => new { file = f, success = s })
        .Where(x => x.success)
        .Select(x => x.file);

IDisposable subscription =
    clearMissedFiles
        .Subscribe(f => File.Delete(f));

这会监视文件系统并定期检查。并行调用API,并不会导致O / S一次尝试删除多个文件。

只需在退出前打电话给subscription.Dispose()进行清理。

NuGet“System.Reactive”为位,然后引用using System.Reactive.Linq为扩展显示。

© www.soinside.com 2019 - 2024. All rights reserved.