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?
– Leandro Angelo
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
– Dorathoto
It would not be better for you to invoke the event after creating the file to process and remove from the input folder?
– Leandro Angelo
@Leandroangelo did not understand...
– Dorathoto
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;
– Rafael