I have built a Windows Service and for some reason, when I start the service it starts up and then shuts right back down. I’ve tried googling why this is happening. Nothing is appearing in any system logs. Here is my Start/Stop code for the service. I would expect that since I have created a File Listener that it should stay running. What am I missing?
#region Declarations
private List<string> _keys = new List<string>();
private FileSystemWatcher _watcher;
private BackgroundWorker _worker;
static private bool _isBusy = false;
#endregion
#region Constructor
public FeedListener()
{
InitializeComponent();
}
#endregion
#region Start/Stop
protected override void OnStart(string[] args)
{
_keys.AddRange(new string[] { "csv", "xml", "zip", "rivx" });
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
_worker.DoWork += new DoWorkEventHandler(BackgroundWorkerDoWork);
_worker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorkerProgressChanged);
_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerRunWorkerCompleted);
_watcher = new FileSystemWatcher(AppSettings.Default.FTPRootPath, "*.*");
_watcher.IncludeSubdirectories = true;
_watcher.NotifyFilter = sysIO.NotifyFilters.DirectoryName | sysIO.NotifyFilters.FileName | sysIO.NotifyFilters.LastAccess | sysIO.NotifyFilters.CreationTime | sysIO.NotifyFilters.LastWrite;
_watcher.Created += new sysIO.FileSystemEventHandler(fileCreatedOrChanged);
_watcher.Changed += new sysIO.FileSystemEventHandler(fileCreatedOrChanged);
_watcher.EnableRaisingEvents = true;
TouchFiles();
}
protected override void OnStop()
{
_watcher.Dispose();
_watcher = null;
_worker.Dispose();
_worker = null;
}
#endregion
#region Event Handlers
void fileCreatedOrChanged(object sender, sysIO.FileSystemEventArgs e)
{
DTO.BackgroundWorkerEventArgs eventArgs = new DTO.BackgroundWorkerEventArgs();
sysIO.WatcherChangeTypes myType = e.ChangeType;
bool isValid = false;
foreach (string key in _keys)
{
if (Path.GetExtension(e.FullPath).Replace(".", "").Equals(key, StringComparison.CurrentCultureIgnoreCase))
isValid = true;
}
if (isValid)
{
try
{
eventArgs.PathAndFile = e.FullPath;
eventArgs.Key = Path.GetExtension(e.FullPath).ToLower().Replace(".", "");
eventArgs.FileName = Path.GetFileName(e.FullPath);
eventArgs.Path = Path.GetDirectoryName(e.FullPath);
eventArgs.UserName = Path.GetDirectoryName(e.FullPath).Replace(AppSettings.Default.FTPRootPath, "").Replace("\\", "");
FileInfo fileInfo = new FileInfo(eventArgs.PathAndFile);
// 1st attempt at stalling for the file lock due to slow write speeds...
while (IsFileLocked(fileInfo)) { /* nop */ }
// Wait until the thread is not busy...
//while (_worker.IsBusy) { /* nop */ }
while (_isBusy) { /* nop */ }
// Now, spin up a new thread and do the work on the file, based on file type...
_worker.RunWorkerAsync(eventArgs); // goes to BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) //
}
catch (Exception ex)
{
string m = ex.Message;
}
}
}
void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
DTO.BackgroundWorkerEventArgs eventArgs = (DTO.BackgroundWorkerEventArgs)e.Argument;
RivWorks.FeedHandler.Handler handler = new RivWorks.FeedHandler.Handler();
_isBusy = true;
try
{
if (eventArgs.Key.Equals("csv", StringComparison.CurrentCultureIgnoreCase))
{
handler.ImportCSV(ref eventArgs);
}
if (eventArgs.Key.Equals("zip", StringComparison.CurrentCultureIgnoreCase))
{
handler.UnZip(ref eventArgs);
handler.ImportCSV(ref eventArgs);
}
}
catch (Exception ex)
{
string m = ex.Message;
_worker.ReportProgress(0);
}
finally
{
_isBusy = false;
_worker.ReportProgress(100);
if (_worker.CancellationPending)
{
e.Cancel = true;
}
}
}
void BackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
void BackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
Console.WriteLine("Cancelled.");
}
else if (e.Error != null)
{
Console.WriteLine(e.Error.Message);
}
else
{
Console.WriteLine("Successfully completed.");
}
TouchFiles();
}
#endregion