DispatchTimer not firing in service - c#

I have a windows service implemented in C#. The service watches a number of directories for a file to be saved. However to make it more robust I am trying to get it to check the folders after a period of time.
The inner class called a Watcher initialises both the fileSystemWatcher and DispatchTimer.
public class Watcher
{
FileSystemWatcher fileSystemWatcher = null;
public delegate void TimerInvokedHandler(object sender);
public event TimerInvokedHandler TimerInvoked;
public DispatcherTimer Timer { get; set; }
public int TimerMinutes { get; set; }
public Watcher()
{
}
public Watcher(String directory, String filter, String dap)
{
this.DAP = dap;
this.Directory = directory;
this.Filter = filter;
}
public Boolean EnableRaisingTimerEvents
{
get { return this.Timer.IsEnabled; }
set
{
this.Timer.IsEnabled = value;
if (value)
{
this.Timer.Start();
Log("Timer Started");
}
else
{
this.Timer.Stop();
Log("Timer Stopped");
}
}
}
private void Timer_Tick(object sender, EventArgs e)
{
StopWatch();
TimerInvoked?.Invoke(sender);
}
public void StartWatch()
{
if (fileSystemWatcher == null)
{
fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Filter = Filter;
fileSystemWatcher.Path = Directory;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
fileSystemWatcher.Error += FileSystemWatcher_Error;
}
if (this.Timer == null)
{
Log("Initialising Timer");
this.Timer = new DispatcherTimer();
this.Timer.Interval = new TimeSpan(0, this.TimerMinutes, 0);
this.Timer.Tick += Timer_Tick;
}
this.EnableRaisingFileSystemsEvents = true;
this.EnableRaisingTimerEvents = true;
Log(String.Format("Watching Directory {0}", Directory));
}
// stops timer and file system events
public void StopWatch()
{
if (fileSystemWatcher != null)
EnableRaisingFileSystemsEvents = false;
if (Timer != null)
{
EnableRaisingTimerEvents = false;
}
Log(String.Format("Watching {0} Switched Off", this.Directory));
}
private void Log ( String Message )
{
LogEvent?.Invoke(this, Message);
}
}
The outer class creates a list of these watchers, this is based on ServiceBase, as in it is a service and so only ends when it gets a stopped within the Windows Service Manager
foreach (nhs_acquisition_profile pfl in p)
{
Watcher w = null;
String filePattern = String.Empty;
try
{
profileWatcherLog.WriteEntry(String.Format("Attempting to set-up watcher on {0} for DAP {1}",pfl.dap_file_location,pfl.dap_name));
if (pfl.dap_acquisition_method_loca.ToLower() == "xml") w = new Watcher(pfl.dap_file_location, "*.xml", pfl.dap_name);
else w = new Watcher(pfl.dap_file_location, "*.*", pfl.dap_name);
profileWatcherLog.WriteEntry("Initialising Event Handlers");
// initialise event handlers
w.FileCreated += W_FileCreated;
w.FileRenamed += W_FileRenamed;
w.TimerInvoked += W_TimerInvoked;
w.LogEvent += W_LogEvent;
profileWatcherLog.WriteEntry("Event Handlers initialised");
// dispatch timer
w.TimerMinutes = Convert.ToInt32(Properties.Settings.Default.TimerDelay);
w.StartWatch();
profileWatcherLog.WriteEntry("Watch started....Adding to Watcher List");
// add the watcher to the list of watchers
FileWatcherList.Add(w);
profileWatcherLog.WriteEntry("Added to list of file watchers");
profileWatcherLog.WriteEntry(String.Format("Watching {0} for files matching *.* for DAP {1}",pfl.dap_file_location,pfl.dap_name));
}
catch
{
throw;
}
}
The variable FileWatcherList is a field of the ProfileWatcher class which forms the service as a whole.
What I am finding is that the DispatchTimer Tick event never happens. I'm reasonable certain that this is not an instance of the DispatchTimer being disposed of before the Tick, but can't see why it is not firing.

Dmitri is correct, I should have been using a Timer and not a DispatchTimer. Once I moved to that it worked fine.

Related

Using NAudio to record fixed length wav, it crashes with "System.AccessViolationException" after the first file

I'm using NAudio to record a fixed length audio file, but it crashes after the first file at the TimerElapsed method.
I'm using a Timer that stops and starts a new recording after the Interval has elapsed.
The filename is changed after each new recording.
This is the error I get.
System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
The class code is here :
namespace AudioService
{
public class AudioRecorder
{
public string Location
{
get => location;
set
{
location = value;
Filename = $"{Location}/test001.wav";
}
}
public string Filename { get; set; }
public double Interval { get; set; } = 10000;
public int DeviceNumber { get; set; } = 0;
private WaveInEvent waveSource = null;
private WaveFileWriter waveFile = null;
public bool LoopRecord { get; set; } = true;
public bool Recording { get; set; } = false;
private System.Timers.Timer timer;
private string location;
public AudioRecorder()
{
Location = Environment.GetEnvironmentVariable("userprofile");
Filename = $"{Location}/test001.wav";
if (LoopRecord) setTimer();
}
private void setTimer()
{
timer = new System.Timers.Timer(Interval);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
/// ****************
/// CRASHES HERE
/// ****************
waveSource?.StopRecording();
Filename = GetNewFileName();
StartRecording(Filename);
}
public void StartRecording()
{
if (timer != null)
timer.Enabled = true;
StartRecording(Filename);
}
public async void StartRecording(string filename)
{
if (waveSource == null)
{
waveSource = new WaveInEvent
{
DeviceNumber = DeviceNumber,
WaveFormat = new WaveFormat(44100, 1)
};
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
}
waveFile = await Task.Run(() => new WaveFileWriter(filename, waveSource.WaveFormat));
waveSource.StartRecording();
Recording = true;
}
private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
Recording = false;
}
private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
waveFile.Flush();
}
}
public void StopRecording()
{
waveSource?.StopRecording();
timer.Enabled = false;
}
public string GetNewFileName()
{
var tempLocation = $"{Location}/{DateTime.Now:yyMMdd}/";
bool folderExists = Directory.Exists(tempLocation);
if (!folderExists)
Directory.CreateDirectory(tempLocation);
string date = DateTime.Now.ToString("HH.mm.ss");
return $"{tempLocation}{date}.wav";
}
}
}
Thanks for helping
That exception basically means that you are crashing the 3rd party unmanaged code in NAudio, probably by trying to access an object that it is busy using or that it has already closed.
Your timer is going to fire every time your interval elapses, but you don't have any code to make sure that the previous recording is complete before starting a new one. In other words, StartRecording will get called over and over one the timer is started. If you want StartRecording to fire ONCE, then timer.AutoReset should be false.
A good way to troubleshoot these kind of issues is to replace all the calls to NAudio objects with console.writelines describing what is happening, and then run your program and observe the output.

Use Timer in another Timer in C#

I have a windows service project with this code:
protected override void OnStart(string[] args)
{
InitializeScheduler();
var timer = new Timer {Interval = 10 * 60 * 1000};
timer.Elapsed += (ss, ee) => InitializeScheduler();
timer.Start();
}
private void InitializeScheduler()
{
_taskScheduler = new TaskScheduler() { Enabled = true };
// do something
}
in TaskScheduler class:
private Timer _triggerTimer;
public TaskScheduler()
{
_triggerTimer = new Timer(1000);
_triggerTimer.Elapsed += (TriggerTimerTick);
}
When I instance "TaskScheduler" in "InitializeScheduler()", _triggerTimer is not dispose and add another timer. How can I dispose it and restart all things in InitializeScheduler() method?
Try to dispose the timer before creating a new one.
private void InitializeScheduler()
{
if( _taskScheduler != null)
_taskScheduler.Dispose();
_taskScheduler = new TaskScheduler() { Enabled = true };
// do something
}
TaskScheduler class:
private Timer _triggerTimer;
public TaskScheduler()
{
_triggerTimer = new Timer(1000);
_triggerTimer.Elapsed += (TriggerTimerTick);
}
public Dispose()
{
if(_triggerTimer != null){
_triggerTimer.Dispose()
_triggerTimer = null;
}

File System Watcher Process called from another class not firing events c#

I have a windows service that basically monitors a folder, copies files to a local directory processes the files. The service starts up fine enough calls the Watcher Class and does all of the configuration like it should. So my code works for the setup. However I get 0 events firing. I am wondering if when my StartFolderWatcher() method goes out of scope that my object - even though declared at the class level is somehow getting disposed.
So do I need to run the object continuously in a separate thread ? If so can I get an example of how I should do that Task.Run(()=> myObject)
Relevant Code is below - if more is needed just ask and I will post it.
static class Program
{
static void Main(string[] args)
{
JobProcessor service = new JobProcessor();
if (Environment.UserInteractive)
{
service.RunAsConsole(args);
}
}
private static bool IsDebugMode(string[] args)
{
if (args == null) return false;
if (args[0].ToLower() == "/debug") return true;
return false;
}
}
public partial class JobProcessor : ServiceBase
{
string[] folders = new string[] {"InBoxFolder"};
HotFolderWatch HFW = new HotFolderWatch();
public JobProcessor()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
StartFolderWatcher();
}
public void StartFolderWatcher()
{
FileWatcherInfo[] ServiceWatchers = new FileWatcherInfo[4];
ServiceConfiguration sc = new ServiceConfiguration();
for (int i = 0; i < folders.Length; i++)
{
ServiceWatchers[i] = sc.GetWatchFolderSettings(folders[i]);
}
HFW = new HotFolderWatch(ServiceWatchers[0]);
HFW.ReadyToProcess += ReadyToProcess;
HFW.InBoxFolderDisconnected += OnInBoxFolderDisconnected;
HFW.LocalFolderDisconnected += OnLocalFolderDisconnected;
HFW.ProcessFolderDisconnected += OnProcessFolderDisconnected;
}
public void RunAsConsole(string[] args)
{
OnStart(args);
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
OnStop();
}
}
public HotFolderWatch(FileWatcherInfo inbox)
{
this.InboxCacheTimeMilliseconds = inbox.CacheTimeMilliseconds;
this.InBoxFolder = inbox.Folder.Trim();
this.InboxFileFilter = inbox.Filter.Trim();
SetInboxWatcher();
}
private void SetInboxWatcher()
{
InBoxWatcher = new FileSystemWatcher(InBoxFolder, InboxFileFilter);
InBoxWatcher.IncludeSubdirectories = false;
InBoxWatcher.NotifyFilter =
NotifyFilters.LastAccess | NotifyFilters.LastWrite;
InboxCache = MemoryCache.Default;
InboxCachePolicy = new CacheItemPolicy()
{
RemovedCallback = OnRemovedFromInBoxCache
};
InBoxWatcher.Created += new FileSystemEventHandler(OnInBoxChanged);
InBoxWatcher.EnableRaisingEvents = true;
}
private void OnInBoxChanged(object source, FileSystemEventArgs e)
{
InboxCachePolicy.AbsoluteExpiration =
DateTimeOffset.Now.AddMilliseconds(InboxCacheTimeMilliseconds);
InboxCache.AddOrGetExisting(e.Name, e, InboxCachePolicy);
}
}
If in my HotFolderWatch Class I create a FileSystemWatcher in a method as below the code works!
private void CreateWatcher()
{
FileSystemWatcher fsw = new FileSystemWatcher(#"C:\Tests\JobQueue\InFolder","*.txt");
fsw.Created += Fsw_Created;
fsw.EnableRaisingEvents = true;
}
private void Fsw_Created(object sender, FileSystemEventArgs e)
{
string ex = e.FullPath;
WatcherChangeTypes ctmp = e.ChangeType;
// throw new NotImplementedException();
}
Turns out the default constructor of FileSystemWatcher uses the filter FileName by default and I was thinking I would get a last Write or Last Access with out concerning myself with the Name.
I received good information from here :
https://www.codeproject.com/Articles/1220094/NotifyFilters-Enumeration-Explained-FileSystemWatc
The article posted in the link is what helped me to solve my issue.
I had to Ste the NotifyFilters.FileName as one of the filters.

FileWatcher typicall implementation?

I am building an app, where I will watching the files. I am pretty fresh in programming so I (maybe) have little issue, because I dont have some real practice. My program its working, but I dont know how is usally implemented in "real" software. Please overview and comment. Because I like to have much as is possible in DateFiles class and less is possible in MainViewModel.
Yes app in build on MVVM base.
My Current state:
In my MainViewModel
public static string ConfigurationFilesSourcePath2;
private void InitializeFiles()
{
// New instance of DateFiles
DF = new DataFiles();
// Path to DateFiles
DF.ConfigurationFilesSourcePath = ConfigurationFilesSourcePath;
// Run Initialization method to establish "filewatching"
DF.InitializeFiles();
// Refresh ListView in View
RefreshFileList();
// Assign a Handler to PropertyChanged event
DF.PropertyChanged += DF_PropertyChanged;
}
// If something change inside DateFiles
private void DF_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
RefreshFileList();
}
// Refresh file list
public void RefreshFileList()
{
fileslist = new ObservableCollection<Files>();
foreach (var item in DF.fileslist)
{
fileslist.Add(item);
}
NotifyPropertyChanged("fileslist");
}
DataFiles class:
public class DataFiles : ViewModelBase
{
public FileSystemWatcher filewatcher;
public string ConfigurationFilesSourcePath;
public ObservableCollection<Files> fileslist { get; set; } = new ObservableCollection<Files>();
public void InitializeFiles()
{
// Create a new FileSystemWatcher
filewatcher = new FileSystemWatcher();
// Set filter to only catch XAL files
filewatcher.Filter = "*.txt";
// Set the path
filewatcher.Path = ConfigurationFilesSourcePath;
// Subscribe to the Created event
filewatcher.Created += new FileSystemEventHandler(FileOnchanged);
filewatcher.Changed += new FileSystemEventHandler(FileOnchanged);
filewatcher.Deleted += new FileSystemEventHandler(FileOnchanged);
filewatcher.Renamed += new RenamedEventHandler(FileOnRenamed);
// Enable the FileSystemWatcher events
filewatcher.EnableRaisingEvents = true;
RefreshFilesList();
}
private void FileOnchanged(object sender, FileSystemEventArgs e)
{
RefreshFilesList();
}
private void FileOnRenamed(object sender, RenamedEventArgs e)
{
RefreshFilesList();
}
public void RefreshFilesList()
{
fileslist.Clear();
DirectoryInfo dir = new DirectoryInfo(ConfigurationFilesSourcePath);
string[] extensions = new[] { ".txt" };
int nof = 0;
foreach (FileInfo file in dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray())
{
nof++;
fileslist.Add(new Files()
{
FileId = nof,
FileName = file.Name,
FileChanged = file.LastWriteTime.ToString(),
FileCreated = file.CreationTime.ToString(),
OnlyNameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name)
});
NotifyPropertyChanged("fileslist");
}
}
There are several issues with your code. Among most important ones - you rebuild whole file list on every change (while FileSystemEventArgs arguments provide you with info about what has really changed and where) and you don't update your ObservableCollection on UI thread. Look at the following code (but keep in mind that is just a sample for you to spot some problems):
public class DataFiles : IDisposable {
public FileSystemWatcher filewatcher;
private readonly object fileListLock = new object();
// you don't need public setter on this
public ObservableCollection<Files> Fileslist { get; } = new ObservableCollection<Files>();
// pass path here, no need to use property
public void InitializeFiles(string path) {
// dispose existing watcher, if any
DisposeWatcher();
// Create a new FileSystemWatcher
filewatcher = new FileSystemWatcher();
// Set filter to only catch XAL files
filewatcher.Filter = "*.txt";
// Set the path
filewatcher.Path = path;
// Subscribe to the Created event
filewatcher.Created += new FileSystemEventHandler(FileOnchanged);
filewatcher.Changed += new FileSystemEventHandler(FileOnchanged);
filewatcher.Deleted += new FileSystemEventHandler(FileOnchanged);
filewatcher.Renamed += new RenamedEventHandler(FileOnRenamed);
// don't RefreshFilesList on UI thread, that might take some time and will block UI
Task.Run(() => RefreshFilesList());
// Enable the FileSystemWatcher events
filewatcher.EnableRaisingEvents = true;
}
private void FileOnchanged(object sender, FileSystemEventArgs e) {
// lock here to avoid race conditions with RefreshFilesList
lock (fileListLock) {
// better use dictionary to avoid looping over all files
// but looping is still much better than rebuilding whole list
var file = Fileslist.FirstOrDefault(c => String.Equals(c.FullPath, e.FullPath, StringComparison.OrdinalIgnoreCase));
if (file != null) {
if (e.ChangeType == WatcherChangeTypes.Deleted)
; // delete
else
; // update file properties
}
else {
// add new, unless event is delete
}
}
}
private void FileOnRenamed(object sender, RenamedEventArgs e) {
lock (fileListLock) {
// better use dictionary to avoid looping over all files
var file = Fileslist.FirstOrDefault(c => String.Equals(c.FullPath, e.OldFullPath, StringComparison.OrdinalIgnoreCase));
if (file != null) {
file.FullPath = e.FullPath;
}
else {
// add new
}
}
}
public void RefreshFilesList() {
// you need to lock here, because there is a race condition between this method and FileOnRenamed \ FileOnChanged,
// and you might lose some updates or get duplicates.
lock (fileListLock) {
// update ObservableCollection on UI thread
OnUIThreadDo(() => {
Fileslist.Clear();
});
DirectoryInfo dir = new DirectoryInfo(filewatcher.Path);
int nof = 0;
var files = new List<Files>();
// just use EnumerateFiles
foreach (FileInfo file in dir.EnumerateFiles("*.txt")) {
nof++;
int tmp = nof;
// if you are working with UI (that is most likely the case if you use ObservableCollection) -
// you need to update that collection from UI thread if you have bound controls
files.Add(new Files() {
FileId = tmp,
FullPath = file.FullName,
FileChanged = file.LastWriteTime,
FileCreated = file.CreationTime,
});
// don't do that
// NotifyPropertyChanged("fileslist");
}
// publish them all to collection on UI thread
OnUIThreadDo(() => {
foreach (var file in files)
Fileslist.Add(file);
});
}
}
private void OnUIThreadDo(Action a) {
if (Application.Current.CheckAccess())
a();
else
Application.Current.Dispatcher.BeginInvoke(a);
}
public void Dispose() {
DisposeWatcher();
}
private void DisposeWatcher() {
if (filewatcher != null) {
filewatcher.EnableRaisingEvents = false;
filewatcher.Created -= FileOnchanged;
filewatcher.Deleted -= FileOnchanged;
filewatcher.Changed -= FileOnchanged;
filewatcher.Renamed -= FileOnRenamed;
filewatcher.Dispose();
}
}
}
public class Files : INotifyPropertyChanged
{ // implement INotifyPropertyChanged, because you need to reflect property changes in UI
public int FileId { get; set; }
public string FullPath { get; set; }
public string FileName => Path.GetFileName(FullPath);
public DateTime FileChanged { get; set; }
public DateTime FileCreated { get; set; }
public string OnlyNameWithoutExtension => Path.GetFileNameWithoutExtension(FullPath);
}

Missing events on catching from FileSystemWatcher C#

I'm trying to create a simple app, what moves all files writed to some directory to other directory. That's my problem: if i write other than 10000 files at once in my directory(small .txt files over 1KB) - some of them not handling to move on output directory. I'm using FileSystemWatcher events handler to solve this problem. Here is my code example:
Class MyProgramm
{
void Process(Object o, FileSystemEventArgs e)
{
//do something with e.Name file
}
void main()
{
var FSW = New FileSystemWatcher{Path = "C:\\InputDir"};
FSW.Created += Process;
FSW.EnableRisingEvents = true;
Thread.Sleep(Timeout.Infinite);
}
}
Finally, we got some files processed, but some of written files stays unprocessed..
Any suggestions?
I had similar issues with the FileSystemWatcher. It seemed to "drop the ball" quite regularly. I went for an alternative solution by extending "ServiceBase", which has been working consistently since it went live as a windows service. Hope this helps:
public partial class FileMonitor : ServiceBase
{
private Timer timer;
private long interval;
private const string ACCEPTED_FILE_TYPE = "*.csv";
public FileMonitor()
{
this.ServiceName = "Service";
this.CanStop = true;
this.CanPauseAndContinue = true;
this.AutoLog = true;
this.interval = long.Parse(1000);
}
public static void Main()
{
ServiceBase.Run(new FileMonitor());
}
protected override void OnStop()
{
base.OnStop();
this.timer.Dispose();
}
protected override void OnStart(string[] args)
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
this.timer = new Timer(new TimerCallback(ProcessNewFiles), autoEvent, interval, Timeout.Infinite);
}
private void ProcessNewFiles(Object stateInfo)
{
DateTime start = DateTime.Now;
DateTime complete = DateTime.Now;
try
{
string directoryToMonitor = "c:\mydirectory";
DirectoryInfo feedDir = new DirectoryInfo(directoryToMonitor);
FileInfo[] feeds = feedDir.GetFiles(ACCEPTED_FILE_TYPE, SearchOption.TopDirectoryOnly);
foreach (FileInfo feed in feeds.OrderBy(m => m.LastWriteTime))
{
// Do whatever you want to do with the file here
}
}
finally
{
TimeSpan t = complete.Subtract(start);
long calculatedInterval = interval - t.Milliseconds < 0 ? 0 : interval - t.Milliseconds;
this.timer.Change(calculatedInterval, Timeout.Infinite);
}
}
}

Categories

Resources