Service with Filesystemwatcher does not listen forever

Asked

Viewed 95 times

2

I’m doing a windows service in C#, which listens to a folder and every time it has an audio file it uploads.

The problem is that it works, but after a few minutes of listening to the folder, it does not cause problem the service is still open, but stops working. If I just give him a stop and a star he’ll sue again.

To try to debug I made it to record a log, txt that also recorded in the windows registry (filled of Try/catch and put to register) But I can’t, I found.

Now I moved the whole project to a Classlibrary so I can create a console program and try to debug, but nda.

My code (only a few parts):

    protected override void OnStart(string[] args)
    {
          FileRepo.VerificaArquivosExistentes().ContinueWith(async result =>
           {
                var fw = new FileRepo(true);
                 await fw.FileWatch();
          });
    }
        public async Task FileWatch()
        {
            var pathAudio = ConfigurationManager.AppSettings["PathGravacao"];
            var codec = ConfigurationManager.AppSettings["Codec"];

            FileSystemWatcher watcher = new FileSystemWatcher
            {
                Path = pathAudio,
                NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName,
                Filter = codec
            };
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            watcher.Changed += Fsw_Changed;
//essa aqui é uma tentativa de força ele ficar na escuta para sempre
            await Task.Delay(Int32.MaxValue);
        }


   internal static void Fsw_Changed(object sender, FileSystemEventArgs e)
    {
        try
        {
            var hashBlob = Guid.NewGuid();
            var fullPath = e.FullPath;
            UploadFileAsync(e.FullPath, hashBlob).GetAwaiter().GetResult();

            DataRepo.GravaRegistroDapperAsync(fullPath, hashBlob).GetAwaiter().GetResult();

            DeletarArquivo(fullPath);
        }
        catch (Exception ex)
        {
            Log log = new Log();
            log.WriteEntry(ex);
            GravarTXT.Gravar("====> ERRO");
            GravarTXT.Gravar("          " + ex.Message);

        }
    }
  • As is the rest of the service... you are giving a Sleep thread and running the task again?

  • not...I just tried with a await Task.Delay(Int32.Maxvalue); to handle the application...but I believe that listening to Filesystemwatcher would run delegate with any modification at any time...but by the way the app for

  • It would not be better for you to invoke the event after creating the file to process and remove from the input folder?

  • @Leandroangelo did not understand...

  • 1

    I don’t know if you need the answer yet, but I noticed by looking at your code that you are only monitoring the changed files, shouldn’t you monitor the created files? No mistake anyway, simply your Watcher is ignoring the new files. Try adding as well: Watcher.Created += Fsw_changed;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.