I wanna make a service that writes on a .txt file whenever you start or stop using your computer.
It can detect computer shutdown, but cannot detect sleeps or wake ups.
Here's the code I use:
public Service1()
{
InitializeComponent();
Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
SystemEvents.PowerModeChanged += OnPowerChange;
CanHandlePowerEvent = true;
}
protected virtual bool OnPowerEvent(System.ServiceProcess.PowerBroadcastStatus powerStatus)
{
if (powerStatus == PowerBroadcastStatus.QuerySuspend || powerStatus == PowerBroadcastStatus.Suspend)
{
WriteToFile("computer turned off at : " + DateTime.Now);
return false;
}
else if (powerStatus == PowerBroadcastStatus.ResumeCritical || powerStatus == PowerBroadcastStatus.ResumeSuspend || powerStatus == PowerBroadcastStatus.ResumeAutomatic)
{
WriteToFile("computer turned on at : " + DateTime.Now);
return true;
}
return true;
}
private void OnPowerChange(object s, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
WriteToFile("computer turned on at : " + DateTime.Now);
break;
case PowerModes.Suspend:
WriteToFile("computer turned off at : " + DateTime.Now);
break;
}
}
I'll be grateful for any help.
note: WriteToFile is a custom method for writing to a .txt file
You can get it working with help of HiddenForm. The changes which are required are:
First: Subscribe to PowerModeChanged even in say HiddenForm_Load
private void HiddenForm_Load(object sender, EventArgs e)
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerChange);
}
Second: Start Message pump using a thread as:
protected override void OnStart(string[] args)
{
//DO work needed in OnStart
// Message Pump will run in separate thread
new Thread(ThreadWork.RunMessagePump).Start();
}
//Thread class to RunMessagePump
public class ThreadWork
{
public static void RunMessagePump()
{
Application.Run(new HiddenForm());
}
}
Third: Check tick box "Allow service to interact with desktop" in Log On tab of properties page.
Reference to example can be found at LINK
This is a bit difficult to explain. I have a thread instantiated within a WindowsForm, the thread is not a priority and does its job if nobody has taken the resource exclusively using Monitor.TryEnter.
But when closing the form I use the FormClosing event to stop the processing, save the processed data before closing, etc. When trying to take the resource with another Monitor.Enter .. the block is produced.
But the funny thing is that in the following way it works!!. Using a new/secondary thread or task to block the internal thread and perform the final job of save buffering.
"Minimal, Complete, and Verifiable example:"
namespace WindowsFormsApplication3
{
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class Form1 : Form
{
private readonly object _syncRoot = new object();
private volatile bool _working = false;
readonly Label lblInfo = new Label();
public Form1()
{
InitializeComponent();
SuspendLayout();
lblInfo.Text = "...";
lblInfo.Location = new Point(0, 0);
Controls.Add(lblInfo);
CheckBox chkBox = new CheckBox();
chkBox.Text = "Use Task inside FormClosing";
chkBox.AutoSize = true;
chkBox.Location = new Point(Width / 2 - chkBox.Width / 2, Height / 2 + chkBox.Height);
chkBox.Click += ChkBox_Click; ;
Controls.Add(chkBox);
ResumeLayout(false);
FormClosing += Form1_FormClosing;
}
private CancellationTokenSource _cancellationTokenS;
private bool _flUseThread = false;
private void Form1_Load(object sender, EventArgs e)
{
_cancellationTokenS = new CancellationTokenSource();
CancellationToken token = _cancellationTokenS.Token;
_working = true;
Task.Run(() =>
{
while (_working)
{
if (token.IsCancellationRequested)
break;
processData(DateTime.Now);
}
}, token);
}
private void ChkBox_Click(object sender, EventArgs e)
{
_flUseThread = ((CheckBox)sender).Checked;
}
private bool _alreadySafedClose = false;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_flUseThread)
{
if (_alreadySafedClose) return;
if (MessageBox.Show(this,
"Do you really want to cancel?", "Question:",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = true; // Avoid close form.
Enabled = false; // Avoid interact with form.
Task.Factory.StartNew(() =>
{
bool lockWasTaken = false;
try
{
_working = false;
// Lock other thread.
Monitor.Enter(_syncRoot, ref lockWasTaken);
_cancellationTokenS.Cancel();
saveBufferResults();
MessageBox.Show("OK with Thread");
}
catch (Exception)
{
// ignored
}
finally
{
// Release lock other thread if already locked.
if (lockWasTaken)
Monitor.Exit(_syncRoot);
Invoke((MethodInvoker)delegate
{
_alreadySafedClose = true;
Close(); //Finally Close.
});
}
});
}
else
e.Cancel = true;
}
else
{
if (MessageBox.Show(this,
"Do you really want to cancel?", "Question:",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
bool lockWasTaken = false;
try
{
_working = false;
_cancellationTokenS.Cancel();
// Lock other thread avoid continue processing.
Monitor.Enter(_syncRoot, ref lockWasTaken); // <------ here it stays blocked!!
saveBufferResults();
MessageBox.Show("OK");
}
catch (Exception)
{
Console.WriteLine("<DEBUG> The data could not be processed.!");
}
finally
{
// Release lock other thread if already locked.
if (lockWasTaken)
Monitor.Exit(_syncRoot);
}
}
else
e.Cancel = true;
}
}
private void processData(DateTime data)
{
//flag to reduce locks..
if (!_working) return;
if (Monitor.TryEnter(_syncRoot))
{
try
{
//To escape quickly
if (_working)
innerProcessFrame(data);
}
catch (Exception)
{
Console.WriteLine("<DEBUG> The data could not be processed.!");
}
finally
{
Monitor.Exit(_syncRoot);
}
}
}
private ulong _dataCounter = 0;
private void innerProcessFrame(DateTime data)
{
Thread.Sleep(100);
Invoke((MethodInvoker)delegate
{
lblInfo.Text = data.Millisecond + " " + _dataCounter;
});
_dataCounter++;
}
private void saveBufferResults()
{
Thread.Sleep(1);
}
}
}
I believe that FormClosing is breaking the synchronization context or blocking the thread of work and
therefore in the event of the thread of work it never reaches the line where it releases the resource
using Monitor.Exit, in the processData(...) method:
My question is why does this happen only when closing the form within the FormClosing event?, because
if I put a button on the form and put the same blocking code everything stops correctly there is no DeadLock, and why does it work creating a secondary thread to do the final work when closing the form ?, and finally if someone knows a better or correct way to solve it.
I've been running into a Callback problems with async programming in WPF .Net 4.5.
There should be a way of doing this code in a more understandable way (I have suppressed a big part of it to make it easier to see what is going on).
I don't think there is a way to simply remove code because I need to call Dispatcher in order to manipulate WPF controls and calls like in TbSequence.Focus() and Utils.ShowMessageBox.
private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
Controller.Busy = true;
System.Threading.Tasks.Task.Run(() =>
{
try
{
Controller.SaveItem();
}
catch (BdlDbException ex)
{
if (ex.ExceptionSubType == DbExceptionSubTypes.UniqueViolation)
{
HandleUniqueViolation(ex);
}
else
{
string errorMessage = "";
errorMessage = ex.Message;
Dispatcher.BeginInvoke(new Action(() => Utils.ShowMessageBox(t_MessageBox.Attention, errorMessage)));
}
}
Controller.Busy = false;
});
}
catch (FieldException ex)
{
if (ex.FieldName == "FirstName")
{
TbFirstName.ValidationError = true;
TbFirstName.ApplyErrorToolTip(ex.Message);
}
}
}
public void Init(UcEdit container, Employee entity = null)
{
Controller.Busy = true;
System.Threading.Tasks.Task.Run(() =>
{
try
{
Controller.Init(entity);
}
catch (BdlEntryNotFoundException ex)
{
HandleNotFoundException(ex);
}
Container.Dispatcher.BeginInvoke(new Action(() =>
{
Container.DataContext = Controller;
// Instructions order for focusing TbSequence after load should be different in case we have an existent item
// because we have to focus the control AFTER it is filled with data, in order to set the caret position correctly.
if (Controller.IsNewItem)
{
this.DataContext = Controller;
TbSequence.Focus();
Controller.Busy = false;
}
else
{
TbSequence.TextChanged += TbSequence_TextChanged;
this.DataContext = Controller;
SetButtons();
}
}));
});
}
private void HandleUniqueViolation(BdlDbException ex)
{
string errorMessage = "";
bool isSequence = false; // if true, it's name.
if (ex.Fields[1] == "Sequence")
{
errorMessage = "There is already an Employee with the sequence \"" + Controller.Item.Sequence + "\".";
isSequence = true;
}
else
{
errorMessage = "There is already an Employee named \"" + Controller.Item.FirstName +
" " + Controller.Item.LastName + "\".";
}
errorMessage += "\r\nLoad it from Database?\r\n(All the changes on this window will be lost.)";
Dispatcher.BeginInvoke(new Action(() =>
{
MessageBoxResult res = Utils.ShowMessageBox(t_MessageBox.Question, errorMessage, MessageBoxButton.YesNo);
switch (res)
{
case MessageBoxResult.Yes:
if (isSequence)
{
System.Threading.Tasks.Task.Run(() =>
{
Controller.GetEmployeeBySequence(Controller.Item.Sequence);
Init(Container, Controller.OriginalItem);
});
}
else
{
System.Threading.Tasks.Task.Run(() =>
{
Controller.GetEmployeeByName(Controller.Item.FirstName, Controller.Item.LastName);
Init(Container, Controller.OriginalItem);
});
}
break;
case MessageBoxResult.No:
break;
}
}));
}
As you can see, there is a major Callback problem here that behaves like this:
Save_Executed (UI) -> HandleUniqueViolation (Task) -> ShowMessageBox (UI) -> Controller.GetEmployeeBySequence (Task) -> Controller.Init ...
And so it goes.
Is there a way to make this code more easy to read?
Thank you.
You're starting off your code by putting (more or less) the entirety of your method body in a call to Task.Run and then explicitly marshalling to the UI thread all over the place within that callback.
Just narrow the scope of your Task.Run call so that your UI code is just outside the call to Run rather than inside both it and a call to Invoke:
private async void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
Controller.Busy = true;
try
{
await Task.Run(() => Controller.SaveItem());
}
catch (BdlDbException ex)
{
if (ex.ExceptionSubType == DbExceptionSubTypes.UniqueViolation)
{
HandleUniqueViolation(ex);
}
else
{
string errorMessage = "";
errorMessage = ex.Message;
Utils.ShowMessageBox(t_MessageBox.Attention, errorMessage);
}
}
Controller.Busy = false;
}
catch (FieldException ex)
{
if (ex.FieldName == "FirstName")
{
TbFirstName.ValidationError = true;
TbFirstName.ApplyErrorToolTip(ex.Message);
}
}
}
Here you're running the actual long running business operation that you have in a thread pool thread, but doing all of your error handling in the UI thread.
You can do the same thing throughout your application. Rather than putting everything into a background thread and then explicitly marshaling, just only ever execute operations in a background thread that should be entirely executed in a background thread.
Here's DoWork:
private void uploadWorker_DoWork(object sender, DoWorkEventArgs e)
{
uploadWorker.ReportProgress(20);
int tiffError = 0;
finalFiles = Directory.GetFiles(AppVars.FinalPolicyImagesFolder);
foreach (string file in finalFiles)
{
if (!file.EndsWith(".tiff"))
{
tiffError = 1;
break;
}
}
uploadWorker.ReportProgress(50);
if (tiffError == 1)
{
MessageBox.Show("There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (finalFiles.Length == 0)
{
MessageBox.Show("There are no TIFF files to be uploaded. Please generate files first.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
pbUpload.Value = 0;
EnableAllButtons();
}
else
{
double count = finalFiles.Length;
int current = 0;
int pbValue = 0;
uploadWorker.ReportProgress(70);
foreach (string file in finalFiles)
{
current = current + 2;
if (file.Contains(".tiff") == true)
{
PolicyNumber = Path.GetFileName(file).Split('_')[0];
basePolicyNumber = PolicyNumber.Remove(PolicyNumber.Length - 2);
basePolicyNumber = basePolicyNumber + "00";
finalPolicyName = Path.GetFileName(file);
PolicyUUID = Transporter.GetPolicyUUID(AppVars.pxCentralRootURL, basePolicyNumber);
if (PolicyUUID == "")
{
MessageBox.Show("The InsightPolicyID for the policy you are trying to upload does not exist in ixLibrary. Please ensure the policy number is correct. If you are sure it should be in ixLibray, please contact IT.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ixLibrarySourceFileURL = AppVars.ixLibraryPolicyAttachmentsURL + finalPolicyName;
UploadToixLibraryErrorCode = Transporter.UploadFileToixLibrary(AppVars.ixLibraryPolicyAttachmentsURL, file);
if (UploadToixLibraryErrorCode != 0)
{
MessageBox.Show("There was an error uploading the file to ixLibrary. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
GeneratePayLoadErrorCode = Transformer.GeneratePayLoad(ixLibrarySourceFileURL, finalPolicyName);
if (GeneratePayLoadErrorCode != 0)
{
MessageBox.Show("There was an error generating the XML for pxCentral. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
pxCentralPOSTErrorCode = Transporter.pxCentralPOST(AppVars.pxCentralRootURL + PolicyUUID, AppVars.pxCentralXMLPayloadFilePath);
pbValue = Convert.ToInt32(((current / count) * 30) + 70);
uploadWorker.ReportProgress(pbValue);
}
}
}
}
}
}
}
}
As soon as it hits the last }, I get the TargetInvocationException was unhandled error here (see comment in code):
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool createdNew = false;
Mutex mutex = new Mutex(true, "MyApplicationMutex", out createdNew);
if (createdNew == true)
{
//error happens here
Application.Run(new frmMain());
}
else
{
MessageBox.Show("The application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
I'm not sure why this started happening all of the sudden. Does anyone know why?
Finally, here's RunWorkerCompleted:
private void uploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
DeleteFinalFiles(finalFiles);
MessageBox.Show("Upload process complete.", "Complete!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
EnableAllButtons();
}
You are calling EnableAllButtons in your DoWork handler. this, presumably, changes the Enabled state of buttons on the form. that is not legal from any other thread than the UI thread. You should make the call to EnableAllButtons in your ProgressChanged event handler or in your RunWorkerCompleted event handler. You're also calling ProgressBar.Value in the DoWork with the code pbUpload.Value = 0.
Also, you should call MessageBox.Show from your UI thread (i.e. in ProgressChanged or RunworkerCompleted handler) so that the MessageBox can be associated with your forms message pump propertly. You should pass a form object to MessageBox.Show to associate the message box with the form so you can't bring the form to the foreground while the message box is shown. e.g.:
MessageBox.Show(this,
"There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
In WinForms, you must only access a control on the thread that created the control. Your DoWork event handler is not running on the thread that created the form (which, of course, is the point). Therefore, you must not access any of the controls on the form in the DoWork handler. Doing so can create unpredictable results.
I had exactly the same problem with TargetInvocation Exception raised after the completion of the background process. Inside the backgroundWorker ProgressChanges Event I have references to controls as shown below`private void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// This function fires on the UI thread so it's safe to edit
// the UI control directly, no funny business with Control.Invoke :)
CurrentState state =
(CurrentState)e.UserState;
txtProgress.Text = state.CrawlStatus;
lblStatus2.Text = state.sStatus;
txtItemsStored.Text = state.TotItems.ToString() + " items";
txtLastRunTime.Text = state.MostRecentGatherDate.ToString();
AppNameKey.SetValue("LastCrawlTime", txtLastRunTime.Text);
}`
The DoWork event reads from a control
private void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
DateTime LastCrawlTime;
try
{
LastCrawlTime = Convert.ToDateTime(txtLastRunTime.Text);
if (lblStatus2.Text != "Status: Running" || (!cmdRunNow.Enabled && cmdStopRun.Enabled)) // run is not currently running or cmdRunNow clicked
{
//lblStatus2.Text = "Status: Running";
GetUpdated(LastCrawlTime,e);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
The RunWorkedrCompleted Event writes to a control:
void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
lblStatus2.Text = "Status: Stopped";
cmdStopRun.Enabled = false;
}
// Check to see if an error occurred in the background process.
else if (e.Error != null)
{
lblStatus2.Text = "Fatal Error while processing.";
}
else
{
// Everything completed normally.
//CurrentState state = (CurrentState)e.UserState;
lblStatus2.Text = "Status: Finished";
}
}
None of these caused problems. What did cause the problem was the attempt to reference e.UserState in the RunWorker_Completed event (commented out above)
I figured out the issue.
The progress bar was exceeding its maximum allowed (of 100).
The problem was that in the code, I was incrementing the progress bar as such:
current = current + 2;
I replaced it with:
current++;
The reason why I was incrementing by 2 was simply for testing purposes.
I am getting this error in my event logs for a service I put into production:
An unhandled win32 exception occurred
in RivWorks.FeedHandler.exe [5496].
Just-In-Time debugging this exception
failed with the following error:
Debugger could not be started because
no user is logged on.
I have it installed and running under a Win NT global account. I have no idea why it is trying to drop into debugging mode. It was built under the Release model. Running on the 4.0 Framework.
When I run on my dev machine, via an EXE entry point instead of the WinSvc entry point, everything runs just fine - BUT - I am already in "debug" mode.
Any ideas of what to look for?
2010-10-21 - NOTE - Changed the code base.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using sysIO = System.IO;
using RivWorks.FeedHandler;
using System.Collections;
namespace RivWorks.FeedHandler.Service
{
public partial class FeedListener : ServiceBase
{
#region Declarations
private List<string> _keys = new List<string>();
private System.Threading.Timer _clock = null;
private FileSystemWatcher _watcher;
private BackgroundWorker _worker;
private Queue<string> _queue = new Queue<string>();
private bool _isDequeueing = false;
#endregion
#region Constructor
public FeedListener()
{
InitializeComponent();
}
#endregion
#region Start/Stop
protected override void OnStart(string[] args)
{
try
{
WriteToEventLog("Enter Start", EventLogEntryType.Information);
_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;
// check every 15 minutes...
_clock = new System.Threading.Timer(Tick, null, 0, 900000);
WriteToEventLog("Exit Start", EventLogEntryType.Information);
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
this.Stop();
}
}
protected override void OnStop()
{
try
{
_watcher.Dispose();
_watcher = null;
_clock.Dispose();
_clock = null;
_worker.Dispose();
_worker = null;
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Event Handlers
void fileCreatedOrChanged(object sender, sysIO.FileSystemEventArgs e)
{
try
{
WriteToEventLog("Enter fileCreatedOrChanged", EventLogEntryType.Information);
if (!_queue.Contains(e.FullPath))
_queue.Enqueue(e.FullPath);
if (!_isDequeueing)
DeQueue();
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Do work on another Thread
void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
try
{
WriteToEventLog("Enter BackgroundWorkerDoWork", EventLogEntryType.Information);
BackgroundWorker bw = sender as BackgroundWorker;
WriteToEventLog("Create Handler", EventLogEntryType.Information);
RivWorks.FeedHandler.Library.Handler handler = new RivWorks.FeedHandler.Library.Handler(Convert.ToBoolean(AppSettings.Default.InProduction), AppSettings.Default.ArchivePath);
WriteToEventLog("Setup Handler", EventLogEntryType.Information);
handler.Keys = _keys;
handler.RootDirectory = AppSettings.Default.RootDirectory;
handler.FtpPath = AppSettings.Default.FTPRootPath;
handler.WorkPath = AppSettings.Default.WorkPath;
handler.ArchivePath = AppSettings.Default.ArchivePath;
handler.EmailHost = AppSettings.Default.EmailHost;
handler.EmailPassword = AppSettings.Default.EmailPassword;
handler.EmailUser = AppSettings.Default.EmailUser;
handler.ErrorNotificationRecipients = AppSettings.Default.ErrorNotificationRecipients;
handler.InProduction = Convert.ToBoolean(AppSettings.Default.InProduction);
Library.DTO.FileHandler fileHandler = new Library.DTO.FileHandler(handler.FtpPath, handler.WorkPath, handler.ArchivePath, (string)e.Argument);
WriteToEventLog("Call Handler.Execute", EventLogEntryType.Information);
handler.Execute(bw, e, fileHandler);
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
finally
{
WriteToEventLog("Exit BackgroundWorkerDoWork", EventLogEntryType.Information);
}
}
void BackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
try
{
if (e.ProgressPercentage >= 100)
{
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
void BackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
if (e.Cancelled)
{
WriteToEventLog("Cancelled.", EventLogEntryType.Warning);
if (e.Error != null)
{
WriteToEventLog(e.Error, EventLogEntryType.Error);
}
}
else if (e.Error != null)
{
WriteToEventLog(e.Error, EventLogEntryType.Error);
}
else
{
WriteToEventLog("Successfully completed.", EventLogEntryType.Information);
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Private Methods
private void Tick(object data)
{
try
{
if (!_isDequeueing)
{
WriteToEventLog("Enter Tick. FTP Root = " + AppSettings.Default.FTPRootPath, EventLogEntryType.Information);
foreach (string key in _keys)
{
List<string> files = Directory.GetFiles(Path.Combine(AppSettings.Default.FTPRootPath), "*." + key, SearchOption.AllDirectories).ToList();
foreach (string fileName in files)
{
if (File.Exists(fileName))
{
// Toss this file name into the Queue...
WriteToEventLog("Call _queue.Enqueue(" + fileName + ")", EventLogEntryType.Information);
if (!_queue.Contains(fileName))
_queue.Enqueue(fileName);
}
}
}
// Now, start handling the list of files...
DeQueue();
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
finally
{
WriteToEventLog("Exit Tick", EventLogEntryType.Information);
}
}
private void DeQueue()
{
try
{
_isDequeueing = true;
WriteToEventLog("Enter DeQueue", EventLogEntryType.Information);
while (_queue.Count > 0)
{
string queuedFile = _queue.Dequeue();
WriteToEventLog("DeQueued " + queuedFile, EventLogEntryType.Information);
bool isValid = false;
foreach (string key in _keys)
{
if (Path.GetExtension(queuedFile).Replace(".", "").Equals(key, StringComparison.CurrentCultureIgnoreCase))
isValid = true;
}
if (isValid)
{
// Now, spin up a new thread and do the work on the file, based on file type...
WriteToEventLog("Call RunWorkerAsync", EventLogEntryType.Information);
string UserName = Path.GetDirectoryName(queuedFile).Replace(AppSettings.Default.FTPRootPath, "").Replace("\\", "");
int i = 0;
DateTime sTime = DateTime.Now;
DateTime eTime = DateTime.Now;
_worker.RunWorkerAsync(queuedFile); // goes to BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) //
while(_worker.IsBusy)
{
System.Threading.Thread.Sleep(5000);
i++;
}
eTime = DateTime.Now;
TimeSpan ts = new TimeSpan(eTime.Ticks - sTime.Ticks);
string msg = String.Format("Import for {0} started at {1} and ended at {2}. It took {3} cycles and the elapsed time was {4}:{5}:{6}.", UserName, sTime, eTime, i, ts.Hours, ts.Minutes, ts.Seconds);
WriteToEventLog(msg, EventLogEntryType.Information);
}
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
finally
{
_isDequeueing = false;
WriteToEventLog("Exit DeQueue", EventLogEntryType.Information);
}
}
private void WriteToEventLog(Exception ex, EventLogEntryType eventLogEntryType)
{
try
{
string message = string.Empty;
string sTrace = ex.StackTrace;
while (ex != null)
{
message = message + Environment.NewLine + Environment.NewLine + ex.Message;
ex = ex.InnerException;
}
message = message + Environment.NewLine + Environment.NewLine + sTrace;
WriteToEventLog(message, eventLogEntryType);
}
catch (Exception ex2)
{
WriteToEventLog(ex2.Message, EventLogEntryType.Error);
}
}
private void WriteToEventLog(string message, EventLogEntryType eventLogEntryType)
{
try
{
this.EventLog.WriteEntry(message, eventLogEntryType);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
2010-10-20 - NOTE - Added the Service code file. Maybe there is an elementary mistake in here?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using sysIO = System.IO;
using RivWorks.FeedHandler;
namespace RivWorks.FeedHandler.Service
{
public partial class FeedListener : ServiceBase
{
#region Declarations
private List<string> _keys = new List<string>();
private System.Threading.Timer _clock = null;
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)
{
try
{
_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;
// check every 5 minutes...
_clock = new System.Threading.Timer(Tick, null, 0, 300000);
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
this.Stop();
}
}
protected override void OnStop()
{
try
{
_watcher.Dispose();
_watcher = null;
_clock.Dispose();
_clock = null;
_worker.Dispose();
_worker = null;
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Event Handlers
void fileCreatedOrChanged(object sender, sysIO.FileSystemEventArgs e)
{
try
{
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)
{
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("\\", "");
eventArgs.IsRunning = true;
System.Threading.Thread.Sleep(30000);
// Now, spin up a new thread and do the work on the file, based on file type...
_isBusy = true;
_worker.RunWorkerAsync(eventArgs); // goes to BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) //
int i = 0;
DateTime sTime = DateTime.Now;
DateTime eTime = DateTime.Now;
while (_isBusy)
{
System.Threading.Thread.Sleep(5000);
i++;
}
eTime = DateTime.Now;
TimeSpan ts = new TimeSpan(eTime.Ticks - sTime.Ticks);
string msg = String.Format("Import for {0} started at {1} and ended at {2}. It took {3} cycles and the elapsed time was {4}:{5}:{6}.", eventArgs.UserName, sTime, eTime, i, ts.Hours, ts.Minutes, ts.Seconds);
WriteToEventLog(msg, EventLogEntryType.Information);
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Do work on another Thread
void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
try
{
RivWorks.FeedHandler.Handler handler = new RivWorks.FeedHandler.Handler();
BackgroundWorker bw = sender as BackgroundWorker;
handler.Execute(bw, e);
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
finally
{
_isBusy = false;
}
}
void BackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
try
{
if (e.ProgressPercentage >= 100)
{
_isBusy = false;
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
void BackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
if (e.Cancelled)
{
WriteToEventLog("Cancelled.", EventLogEntryType.Warning);
if (e.Error != null)
{
WriteToEventLog(e.Error, EventLogEntryType.Error);
}
}
else if (e.Error != null)
{
WriteToEventLog(e.Error, EventLogEntryType.Error);
}
else
{
WriteToEventLog("Successfully completed.", EventLogEntryType.Information);
}
_isBusy = false;
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
#region Private Methods
private void Tick(object data)
{
try
{
foreach (string key in _keys)
{
List<string> files = Directory.GetFiles(Path.Combine(AppSettings.Default.FTPRootPath), "*." + key, SearchOption.AllDirectories).ToList();
foreach (string fileName in files)
{
System.Threading.Thread.Sleep(5000);
if (File.Exists(fileName))
{
DateTime lat = File.GetLastWriteTime(fileName);
try
{
File.SetLastWriteTime(fileName, DateTime.Now);
}
catch
{
// just catch and ignore with a short pause...
System.Threading.Thread.Sleep(5000);
}
}
}
}
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
private void WriteToEventLog(Exception ex, EventLogEntryType eventLogEntryType)
{
try
{
string message = string.Empty;
string sTrace = ex.StackTrace;
while (ex != null)
{
message = message + Environment.NewLine + Environment.NewLine + ex.Message;
ex = ex.InnerException;
}
message = message + Environment.NewLine + Environment.NewLine + sTrace;
this.EventLog.WriteEntry(message, eventLogEntryType);
}
catch (Exception ex2)
{
WriteToEventLog(ex2, EventLogEntryType.Error);
}
}
private void WriteToEventLog(string message, EventLogEntryType eventLogEntryType)
{
try
{
this.EventLog.WriteEntry(message, eventLogEntryType);
}
catch (Exception ex)
{
WriteToEventLog(ex, EventLogEntryType.Error);
}
}
#endregion
}
}
Even though it's running as a release exe, you'll still be given the option to attach to a debugger when the app crashes... you just won't see debug symbols, just assembly :)
I believe it's the Dr. Watson process that catches app errors for debugging... Because your app is a service, Dr. Watson can't interact with the desktop, giving you the error you see. You can go to the service properties and mark "allow service to interact with the desktop", found on the LogOn tab, which should then give you a Dr. Watson popup when the app crashes.
Steps to disable Dr. Watson are here:
http://support.microsoft.com/kb/188296
If you want to debug the app on the server, you can enable remote debugging on the server, and attach Visual Studio to the process... if you want to try this, I can give you more tips for debugging a windows service remotely.
HTH,
James
* Edit *
Based on the code you provided, I'd look at the following areas:
Is AppSettings.Default.FTPRootPath set correctly in App.Config?
Are there changes happening to that directory immediately when the service starts? You have a timer commented as "check every five minutes", which is a little confusing, because the FileSystemWatcher will start receiving events as soon as you set EnableRaisingEvents to true. So the issue could actually lie within fileCreatedOrChanged
Along those lines, you have one BackgroundWorker servicing multiple events, and worse, you're firing the handler asynchronously. This is my most likely suspect, because if you call _worker.RunWorkerAsync() again while the first job is running, you'll get an InvalidOperationException. Though I'm not sure why you wouldn't see that in the log
You're using the timer to update the last write time for all files in the watched directory, and you do this every five seconds. This seems like a very bad idea... I'm not sure what you're trying to accomplish. This will fire your FileSystemWatcher's changed event, which would explain why you're crashing less than 10 seconds after you start (the timer's initial tick is set to fire immediately, meaning five seconds later you're changing all the file times, triggering the FileSystemWatcher multiple times shortly after that)
So my best guess is that within five seconds, you've begun firing multiple RunWorkAsync() calls on the same BackgroundWorker, which is a no-no : )
Setting the static variable _isBusy to true/false isn't reliable because you're multi-threading with the BackgroundWorkers... you need to use a Mutex or some other lock, but doesn't that really defeat the purpose of using a BackgroundWorker?
Also, if you wanted to use something like an isBusy flag, it would have to look more like:
while (_isBusy) {
System.Threading.Thread.Sleep(5000);
}
_isBusy = true;
_worker.RunWorkerAsync(eventArgs);
You need _isBusy to be false before you try to launch the Background worker... the way you have it, if the event fires 100 times, you'll make 100 calls.
The easiest solution to your problem would be to create a new BackgroundWorker in the fileCreatedOrChanged method every time the event fires... there's overhead involved in creating so many new threads, but if the work being done in this method is significant, it will be worth the overhead.
You might be able to rely on the built-in BackgroundWorker.IsBusy property, but again, I'd have to question the benefit of asynchronous threading if you're just going to block until the background worker completes.
** Edit **
I understand now what you're trying to accomplish with the initial file timestamp changes... I think you would do better to leave the timestamps alone, but just run through a startup loop to process existing files. You can spawn a background worker thread for each one, just like you do on the FileSystemWatcher nofications. The way you're handling it is deliberately creating a side-effect to trigger the result you want.
I'm losing track a little bit in the growing complexity... the whole queue/dequeue thing might be unnecessary. Or maybe I just am not seeing a need that is truly there. Again, what strikes me is that you are launching the background worker asynchronously, then putting the main thread to sleep until it finishes.
When you put the main thread to sleep, no events will get processed, so you are truly throttling the multi-threading to one thread. I see that you want to write to the event log how long it took for a thread to finish. I will start a second answer to address that if I get a chance to, but the gist of it is to pass a Stopwatch class (which will give you an accurate count of either milliseconds or CPU ticks that pass during an operation) to the DoWorkEventArgs.Result property.
But the code you requested! Basically, wherever you decide to call _worker.RunWorkerAsync(queuedFile), rather than run one class-level BackgroundWorker create a new one each time. Pass all the same parameters for the event handlers, etc. Your service entry point would drop all the BGW references and look like:
protected override void OnStart(string[] args)
{
try
{
_keys.AddRange(new string[] { "csv", "xml", "zip", "rivx" });
_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;
WriteToEventLog("Exit Start", EventLogEntryType.Information);
}
and the code where you run the BGW asynchronously would become:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(BackgroundWorkerDoWork);
worker.ProgressChanged += BackgroundWorkerProgressChanged; // Note you don't need
worker.RunWorkerCompleted += BackgroundWorkerRunWorkerCompleted; // the 'new' here
worker.RunWorkerAsync(queuedFile); // goes to BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) //
The error message tells you that it couldn't attach a debugger to let you inspect the exception. This is completely unrelated to the fact that this is a release build. Release build and debug builds can both be debugged (fortunately!).
Debugging services is a little different from debugging regular applications. Please check this guide for some advice.