I wrote a little program which I run as a Windows-Service, which moves Files of an Inbox-Folder to another location on receive.
But randomly it stops working (Service still runing, process not crashing). If I just move them out the inbox and back, it starts again.
Any hints to improve?
//CIPCoyService.cs
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
namespace CopyCIP
{
public partial class CIPCopyService : ServiceBase
{
public CIPCopyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// start the triggers
initXML();
initPDF();
initCSV();
}
private static void initXML()
{
FileSystemWatcher WatcherXML = new FileSystemWatcher();
WatcherXML.Path = #"C:\xxx\baseDir\inbox";
WatcherXML.IncludeSubdirectories = false;
WatcherXML.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
WatcherXML.Filter = "*.xml";
WatcherXML.Created += new FileSystemEventHandler(Watcher_ChangedXML);
WatcherXML.EnableRaisingEvents = true;
}
private static void initPDF()
{
FileSystemWatcher WatcherPDF = new FileSystemWatcher();
WatcherPDF.Path = #"C:\xxx\baseDir\inbox";
WatcherPDF.IncludeSubdirectories = false;
WatcherPDF.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
WatcherPDF.Filter = "*.pdf";
WatcherPDF.Created += new FileSystemEventHandler(Watcher_ChangedPDF);
WatcherPDF.EnableRaisingEvents = true;
}
private static void initCSV()
{
FileSystemWatcher WatcherCSV = new FileSystemWatcher();
WatcherCSV.Path = #"C:\xxx\baseDir\inbox";
WatcherCSV.IncludeSubdirectories = false;
WatcherCSV.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
WatcherCSV.Filter = "*.csv";
WatcherCSV.Created += new FileSystemEventHandler(Watcher_ChangedCSV);
WatcherCSV.EnableRaisingEvents = true;
}
private static void Watcher_ChangedXML(object sender, FileSystemEventArgs e)
{
//move the XML stuff
string XMLPath = #"\\xxx\d$\CIP\Admin\Prod\SN\Import\Eingang\eSCHKG\";
if (File.Exists(e.FullPath))
// Ensure that the target does not exist.
if (File.Exists(Path.Combine(XMLPath,e.Name)))
File.Delete(Path.Combine(XMLPath, e.Name));
WaitReady(e.FullPath);
try
{
File.Move(e.FullPath, Path.Combine(XMLPath, e.Name));
}
catch (IOException ex)
{
eventWriteEx(ex, "XML");
}
}
private static void Watcher_ChangedPDF(object sender, FileSystemEventArgs e)
{
// move the PDF stuff
string PDFPath = #"\\xxx\d$\CIP\Admin\Prod\SN\Import\Eingang\eSCHKG\PDF\";
if (File.Exists(e.FullPath))
// Ensure that the target does not exist.
if (File.Exists(Path.Combine(PDFPath, e.Name)))
File.Delete(Path.Combine(PDFPath, e.Name));
WaitReady(e.FullPath);
try
{
File.Move(e.FullPath, Path.Combine(PDFPath, e.Name));
}
catch (IOException ex)
{
eventWriteEx(ex, "PDF");
}
}
private static void Watcher_ChangedCSV(object sender, FileSystemEventArgs e)
{
// move the CSV stuff
string CSVPath = #"\\xxx\d$\CIP\Admin\Prod\SN\Import\Eingang\eSCHKG\CSV\";
if (File.Exists(e.FullPath))
// Ensure that the target does not exist.
if (File.Exists(Path.Combine(CSVPath, e.Name)))
File.Delete(Path.Combine(CSVPath, e.Name));
WaitReady(e.FullPath);
try
{
WaitReady(e.FullPath);
File.Move(e.FullPath, Path.Combine(CSVPath, e.Name));
}
catch (Exception ex)
{
eventWriteEx(ex, "CSV");
}
}
private static void eventWriteEx(Exception ex, string what)
{
string eSource = "CIPCopyService";
if (!EventLog.SourceExists(eSource))
EventLog.CreateEventSource(eSource, "CIPEvents");
EventLog.WriteEntry("CIPCopy Exception" + what, ex.Message + "Trace" + ex.StackTrace, EventLogEntryType.Error);
}
private static void eventWriteInfo(string what)
{
string eSource = "CIPCopyService";
if (!EventLog.SourceExists(eSource))
EventLog.CreateEventSource(eSource, "CIPEvents");
EventLog.WriteEntry(eSource, what);
}
public static void WaitReady(string fileName)
{
while (true)
{
try
{
using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (stream != null)
{
eventWriteInfo(string.Format("Output file {0} ready.", fileName));
break;
}
}
}
catch (FileNotFoundException ex)
{
eventWriteEx(ex, string.Format("Output file {0} not yet ready ({1})"));
}
catch (IOException ex)
{
eventWriteEx(ex, string.Format("Output file {0} not yet ready ({1})"));
}
catch (UnauthorizedAccessException ex)
{
eventWriteEx(ex, string.Format("Output file {0} not yet ready ({1})"));
}
Thread.Sleep(500);
}
}
protected override void OnStop()
{
}
}
}
FileSystemWatcher has a limitation if many files are touched in a short amount of time. The internal buffer of the FileSystemWatcher is full and any changes that occur until the buffer has free space can't get handled. You can increase the buffer to a maximum of 65536:
watcherXML.InternalBufferSize = 65536;
But even this has a limitation. If you expect really huge amounts of files you should probably use a Timer and move all existing files to the new location;
private static void initXML()
{
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += (y,z) =>
{
foreach(string file in Directory.GetFiles(#"C:\xxx\baseDir\inbox")
{
MoveFile(file);
}
};
timer.Start();
}
private void MoveFile(string file)
{
string XMLPath = #"\\xxx\d$\CIP\Admin\Prod\SN\Import\Eingang\eSCHKG\";
string fileName = Path.GetFileName(file);
if (File.Exists(file))
// Ensure that the target does not exist.
if (File.Exists(Path.Combine(XMLPath,fileName )))
File.Delete(Path.Combine(XMLPath, fileName ));
WaitReady(file);
try
{
File.Move(file, Path.Combine(XMLPath, fileName));
}
catch (IOException ex)
{
eventWriteEx(ex, "XML");
}
}
Related
I would like to understand the root cause for this error:
Unable to remove the file to be replaced.
This error comes while executing File.Replace(), and having in parallel in another application an active FileSystemWatcher, which as a reaction to the events, is calling Path.GetDirectoryName() for the actual element received in the eventargs.
The code for the two console applications looks like this:
FileReplacerr:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
internal static class FileReplacer
{
private static void Main(string[] args)
{
Console.WriteLine("Test Started.");
Directory.CreateDirectory(rootPath);
CleanUp(new List<string> { livePath, tempPath }, -1);
for (int loopIndex = 0; loopIndex < 10000; loopIndex++)
{
WriteXmlFile(livePath + loopIndex, "LiveContent");
WriteXmlFile(tempPath + loopIndex, "TempContent");
ReplaceFiles(livePath + loopIndex, tempPath + loopIndex);
CleanUp(new List<string> { livePath + loopIndex, tempPath + loopIndex }, loopIndex);
}
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Test Finished.");
Console.WriteLine("Exception count: {0}", exceptionCount);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static void ReplaceFiles(string replacedFile, string replacementFile)
{
try
{
File.Replace(replacedFile, replacementFile, null);
}
catch (Exception ex)
{
exceptionCount++;
string extendedErrorMessage = string.Format("Replacement of original:'{0}' and temporary:'{1}' xml file completed with error code: {2}", replacedFile, replacementFile, ex.Message);
Console.WriteLine("Exception happened during replace:");
Console.WriteLine(extendedErrorMessage);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
private static void WriteXmlFile(string filePath, string elementText)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 32 * 1024, FileOptions.SequentialScan))
{
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.SetLength(0);
XmlWriterSettings settings = new XmlWriterSettings { CheckCharacters = false };
using (XmlWriter xmlWriter = XmlWriter.Create(fileStream, settings))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(elementText);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
}
private static void CleanUp(List<string> filePaths, int loopIndex)
{
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
}
private static int exceptionCount = 0;
static readonly string rootPath = #"C:\teststorage\";
static readonly string liveFile = "live.xml";
static readonly string tempFile = "temp.xml.tmp";
static readonly string livePath = rootPath + liveFile;
static readonly string tempPath = rootPath + tempFile;
}
}
FileSystemWatcherr:
using System;
using System.IO;
namespace FileSystemWatcherr
{
class FileSystemWatcherr
{
static void Main(string[] args)
{
using (FileSystemWatcher watcher = new FileSystemWatcher
{
Path = #"c:\",
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName,
IncludeSubdirectories = true
})
{
watcher.Changed += OnFolderStructureChanged;
watcher.Created += OnFolderStructureChanged;
watcher.Deleted += OnFolderStructureChanged;
watcher.Renamed += OnFolderStructureChanged;
watcher.EnableRaisingEvents = true;
Console.WriteLine("FileSystemWatcher is active");
Console.WriteLine("Press any key to stop watching and exit");
Console.ReadKey();
watcher.EnableRaisingEvents = false;
watcher.Changed -= OnFolderStructureChanged;
watcher.Created -= OnFolderStructureChanged;
watcher.Deleted -= OnFolderStructureChanged;
watcher.Renamed -= OnFolderStructureChanged;
}
}
private static void OnFolderStructureChanged(object sender, FileSystemEventArgs e)
{
string path = string.Empty;
switch (e.ChangeType)
{
case WatcherChangeTypes.Changed:
case WatcherChangeTypes.Deleted:
case WatcherChangeTypes.Renamed:
case WatcherChangeTypes.Created:
default:
path = Path.GetDirectoryName(e.FullPath);
break;
}
}
}
}
Just start the FileSystemWatcherr and then start the FileReplacerr and you will see the error happening.
The issue happens more frequently on Win7 machines. On my Win10 machine, it did not happen, but running the applications on a virtual machine with Win10, the issue happens many times during 10000 loops.
Does anyone have an idea what could be the root cause?
Up to now, I tried using Process Monitor, but from its output I do not understand what the root cause could be:
If you have an idea how to find the root-cause, or you know it, let me know :)
I'm developping an app in C# using microsoft visual studio (windows form).
What i want to do is to manage different environment through one GUI.
Thus, my gui have to start asynchronously some process (which are commandline applications).
The problem is that I can get the standard output of the process only once it's finished, meaning I can't show what the process is doing in runtime.
since the applications I want to run can take quite a long runtime (uploading big files ...) i would like to display the process output in runtime.
Thus, i created a backgroundworker to separate my gui from the process, and i tried to use a temporary file where the process output is written.
then using a FileSystemWatcher, I could use the "change" event to display the messages in my GUI.
My problem is that since the temporary file is open for writting, i can't read from it at the same time.
Here is my code, does anyone have a way to bypass this problem ? or an other way to do it ?
public partial class Form1 : Form
{
Boolean done = false;
private FileSystemWatcher observateur;
public Form1()
{
InitializeComponent();
// delete the temporary file if existing
if (System.IO.File.Exists("C:\\testoutput.txt"))
{
try
{
System.IO.File.Delete("C:\\testoutput.txt");
}
catch (System.IO.IOException exept)
{
Console.WriteLine(exept.Message);
return;
}
}
File.Create("C:\\testoutput.txt");
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler
(backgroundWorker1_ProgressChanged);
observateur = new FileSystemWatcher();
observateur.Filter = "C:\\testoutput.txt";
observateur.Changed += new FileSystemEventHandler(this.OnChanged);
observateur.Created += new FileSystemEventHandler(this.OnCreate);
}
private void OnChanged(object source, FileSystemEventArgs e)
{
// I tried to bypass the problem of having the file opened by copying it but i doesn't work
File.Copy("C:\\testouput.txt", "C:\\TEMP.txt", true);
}
private void OnCreate(object source, FileSystemEventArgs e)
{
Console.WriteLine("Created");
}
private void button3_Click(object sender, EventArgs e)
{
string outputworker = "";
backgroundWorker1.RunWorkerAsync(outputworker);
while (!done)
{
string text = System.IO.File.ReadAllText("C:\\TEMP.txt");
Thread.Sleep(200);
}
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
outputTextArea.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] args = { "/k " };
string outputWork = e.Argument as string;
backgroundWorker1.ReportProgress(10);
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WorkingDirectory = "C:\\XXXXXXXXXX";
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
int nArgs = args.Length;
if (nArgs > 0)
{
process.StartInfo.Arguments = args[0];
}
for (int i = 1; i < nArgs; i++)
{
process.StartInfo.Arguments = String.Concat(process.StartInfo.Arguments, " && ", args[i]);
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
backgroundWorker1.ReportProgress(20);
process.Start();
backgroundWorker1.ReportProgress(40);
System.IO.StreamWriter sIn = process.StandardInput;
sIn.WriteLine("ExternalCommandLineApp1.exe >> C:\\testoutput.txt");
backgroundWorker1.ReportProgress(60);
sIn.WriteLine("ExternalCommandLineApp1.exe >> C:\\testoutput.txt");
System.IO.StreamReader sOut = process.StandardOutput;
backgroundWorker1.ReportProgress(90);
sIn.WriteLine("EXIT");
outputWork = sOut.ReadToEnd();
process.Close();
backgroundWorker1.ReportProgress(100);
e.Result = outputWork;
done = true;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string output = e.Result as string;
//outputTextArea.Text = output;
}
}
This is not the best way as mentioned in other answers, but it still can work successfully.
You can open a file for reading/writing without blocking other reads/writes. Just use File.Open instead of helper methods and provide additional parameters (FileMode and FileShare)
Here is a complete example. Note that one thread keeps file opened for writing and second thread opens and closes file every time and reads all lines:
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string fileName = "c:\\_data\\temp.txt";
Task writer = new Task(() => {
using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
using (StreamWriter sw = new StreamWriter(fs))
{
for (int i = 0; i < 50; i++ )
{
sw.WriteLine(DateTime.Now.Millisecond.ToString());
sw.Flush();
Thread.Sleep(500);
}
}
});
Task reader = new Task(() => {
for (int i = 0; i < 50; i++)
{
Thread.Sleep(500);
Console.WriteLine("Read again");
if (File.Exists(fileName))
{
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
using (StreamReader r = new StreamReader(fs))
{
while (!r.EndOfStream)
{
Console.WriteLine(r.ReadLine());
}
}
}
}
});
writer.Start();
reader.Start();
writer.Wait();
reader.Wait();
}
}
}
The simplest way with what you've already got is to exploit the UserState you can pass with the BackgroundWorker.
In the backgroundWorker1_DoWork method, you can use
backgroundWorker1.ReportProgress(0, "Whatever text you want to send right now.");
And in backgroundWorker1_ProgressChanged, you can read the message and put it in the text box like this:
outputTextArea.AppendText((e.UserState as string) + "\r\n");
This is a bit inefficient, but it should be much safer and faster than your original solution anyway.
In .NET, you've got many options of passing data between threads. If you want to learn more about the concepts, problems and solutions of multi-threading, you can give this a go: http://www.albahari.com/threading/
You can get the Standard Output of processes using System.Diagnostics.Process StandardOutput property (it's a Stream).
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standardoutput(v=vs.90).aspx
I suggest you use Windows Communications Foundation to do this.
Following is a complete example.
There are two helper classes that you would normally put into a class library for reuse, class WcfServiceHost<T> and class WcfServiceProxy<T>.
This is a console app which you should run from the command line twice, passing a parameter of monitor for the first instance you start, and worker for the second instance.
Run it from the command like like this (assuming the app is called ConsoleApp1.exe):
start ConsoleApp1.exe monitor
start ConsoleApp1.exe worker
and then look at the output. The monitor instance is waiting for progress reports from the worker. The worker instance is reporting the progress, effectively by calling a function in the monitor instance (RPC, or Remote Procedure Call).
Here's the complete code. You will need to reference System.ServiceModel:
using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Threading;
namespace Demo
{
[ServiceContract]
interface IProgressReporter
{
[OperationContract]
void ReportProgress(double percentComplete, string message);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
sealed class Monitor: IProgressReporter
{
public void ReportProgress(double percentComplete, string message)
{
Console.WriteLine("Monitor received progress - Completed {0}%: {1}", percentComplete, message);
if (percentComplete == 100)
{
Program.ReportFinished();
}
}
}
public sealed class WcfServiceHost<T>: IDisposable where T: class
{
public WcfServiceHost(T service, string wcfEndpointAddress)
{
_service = service;
_wcfEndpointAddress = wcfEndpointAddress;
var serviceHost = new ServiceHost(service);
serviceHost.AddServiceEndpoint(typeof(T), new NetNamedPipeBinding(), wcfEndpointAddress);
serviceHost.Open();
_serviceHost = serviceHost;
}
public T Service
{
get
{
return _service;
}
}
public string WcfEndpointAddress
{
get
{
return _wcfEndpointAddress;
}
}
/// <summary>Disposal.</summary>
public void Dispose()
{
if (_serviceHost != null)
{
try
{
_serviceHost.Close();
}
catch (Exception exception) // Don't allow exceptions to escape from Dispose().
{
Trace.WriteLine("There was an exception while closing the host: " + exception.Message);
}
}
}
private readonly T _service;
private readonly string _wcfEndpointAddress;
private readonly ServiceHost _serviceHost;
}
public sealed class WcfServiceProxy<T>: IDisposable where T: class
{
public WcfServiceProxy(string wcfEndpointAddress)
{
_wcfEndpointAddress = wcfEndpointAddress;
_channelFactory = new ChannelFactory<T>(new NetNamedPipeBinding(), _wcfEndpointAddress);
_service = _channelFactory.CreateChannel();
_comms = _service as ICommunicationObject;
if (_comms == null)
{
throw new InvalidOperationException("proxy does not implement ICommunicationObject.");
}
}
public T Service
{
get
{
return _service;
}
}
public string WcfEndpointAddress
{
get
{
return _wcfEndpointAddress;
}
}
public void Dispose()
{
closeComms();
closeChannelFactory();
}
private void closeComms()
{
try
{
_comms.Close();
}
catch (CommunicationException exception) // Not closed - call Abort to transition to the closed state.
{
Debug.WriteLine("CommunicationException while closing ICommunicationObject: " + exception.Message);
_comms.Abort();
}
catch (TimeoutException exception) // Not closed - call Abort to transition to the closed state.
{
Debug.WriteLine("TimeoutException while closing ICommunicationObject: " + exception.Message);
_comms.Abort();
}
catch (Exception exception) // Not closed - call Abort to transition to the closed state.
{
Trace.WriteLine("Unexpected exception while closing ICommunicationObject: " + exception.Message);
_comms.Abort();
}
}
private void closeChannelFactory()
{
try
{
_channelFactory.Close();
}
catch (CommunicationException exception) // Not closed - call Abort to transition to the closed state.
{
Debug.WriteLine("CommunicationException while closing ChannelFactory: " + exception.Message);
_channelFactory.Abort();
}
catch (TimeoutException exception) // Not closed - call Abort to transition to the closed state.
{
Debug.WriteLine("TimeoutException while closing ChannelFactory: " + exception.Message);
_channelFactory.Abort();
}
catch (Exception exception) // Not closed - call Abort to transition to the closed state.
{
Trace.WriteLine("Unexpected exception while closing ChannelFactory: " + exception.Message);
_channelFactory.Abort();
}
}
private readonly T _service;
private readonly string _wcfEndpointAddress;
private readonly ChannelFactory<T> _channelFactory;
private readonly ICommunicationObject _comms;
}
internal static class Program
{
static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "worker")
runWorker();
else
runMonitor();
Console.WriteLine("\nEnded. Press a key to exit.");
Console.ReadKey();
}
public static void ReportFinished()
{
finished.Set();
}
static void runMonitor()
{
using (new WcfServiceHost<IProgressReporter>(new Monitor(), SERVICE_PIPE_NAME))
{
finished.WaitOne();
}
}
static void runWorker()
{
using (var proxy = new WcfServiceProxy<IProgressReporter>(SERVICE_PIPE_NAME))
{
for (int i = 0; i <= 100; ++i)
{
Thread.Sleep(100);
Console.WriteLine("Worker reporting progress: Completed {0}%: {1}", i, i);
proxy.Service.ReportProgress(i, i.ToString());
}
}
}
private static ManualResetEvent finished = new ManualResetEvent(false);
private const string SERVICE_PIPE_NAME = "net.pipe://localhost/MyServicePipeName";
}
}
thanks to you i managed to do what i wanted ^^
Since it took me quite some time to search/debug, I share my solution.
I used a temporary text file, so it's not very "professional" but it works.
To run the process, you have to call :
string[] args = { "/c cmd1", "cmd2" , "cmd3"};
backgroundWorker1.RunWorkerAsync(args);
(sync on a button pressed event for example)
public partial class Form1 : Form
{
string fileName = "c:\\temp\\tempoutput.txt";
public Form1()
{
InitializeComponent();
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler
(backgroundWorker1_ProgressChanged);
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// This function fires on the UI thread so it's safe to edit the UI control directly
progressBar1.Value = e.ProgressPercentage;
readTempFile();
//outputTextArea.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// command line
string[] args = e.Argument as string[];
backgroundWorker1.ReportProgress(2);
try
{
FileStream fs = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("### Starting the process : ###");
sw.Flush();
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WorkingDirectory = "WorkdirPath";
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
// create the command line
int nArgs = args.Length;
if (nArgs > 0)
{
process.StartInfo.Arguments = args[0];
}
for (int i = 1; i < nArgs; i++)
{
process.StartInfo.Arguments = String.Concat(process.StartInfo.Arguments, " && ", args[i]);
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
backgroundWorker1.ReportProgress(5);
process.Start();
backgroundWorker1.ReportProgress(10);
System.IO.StreamWriter sIn = process.StandardInput;
System.IO.StreamReader sOut = process.StandardOutput;
backgroundWorker1.ReportProgress(15);
int timeCount = 15;
string tempOut = "";
while (!sOut.EndOfStream)
{
tempOut = sOut.ReadLine();
sw.WriteLine(tempOut);
sw.Flush();
if (timeCount < 90)
{
// increasing the progress bar value.
//timeCount += 1;
}
backgroundWorker1.ReportProgress(timeCount);
}
sw.WriteLine("Closing process");
sw.Flush();
process.Close();
backgroundWorker1.ReportProgress(100);
}
catch (System.IO.IOException exept)
{
Console.WriteLine(exept.Message);
return;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
readTempFile();
}
private void readTempFile()
{
try
{
FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader r = new StreamReader(fs);
string output = r.ReadToEnd();
outputTextArea.Text = output;
}
catch (System.IO.IOException exept)
{
Console.WriteLine(exept.Message);
return;
}
}
}
Basically I need to communicate with a COM serial printer. There are 2 timers, one will print one ticket at time, and the others will hang up checking for errors and resuming the printing when the errors are gone.
For example: Printer is printing but if I press the pause button an error (read in the buffer) will get some booleans to 1 and after I read them I am able to tell what the error is and stop the printing.
My problem is in the method ErrorTimer_Elapsed() which is the timer elapsed event and probably is linked to the ErrorCheck and SendToCom (which is the printing method). The problem is that program detects errors well, but when in ErrorTimer_Elapsed() I try to repeatdly check for error status changes, I get IO errors like:
-Can't acces COM PORT
-The resource you required is in use
and so on.
Can you help me? Thanks, here's the code (about 180 lines), I'm bumping my head on this since 3 days :( :
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
#region Variabili di istanza
private ArrayList _filesToPrint = new ArrayList();
private static SerialPort _serialPort1 = new SerialPort();
private string _portName = ConfigurationSettings.AppSettings["COM"];
private string FilesPath = ConfigurationSettings.AppSettings["Path"];
private System.Timers.Timer PrintTimer = new System.Timers.Timer(2000);
private System.Timers.Timer ErrorTimer = new System.Timers.Timer(3500);
private bool PrintTimerIsActive = true;
private string HsCOMerrorStrings, PrintErrors;
#endregion
public Form1()
{
_serialPort1.PortName = _portName;
_serialPort1.ReadTimeout = 1000;
_IfFileExistsPrint();
}
private void _IfFileExistsPrint()
{
foreach (string file in Directory.GetFiles(FilesPath))
_filesToPrint.Add(file);
FileSystemWatcher SysWatch = new FileSystemWatcher(FilesPath, "*.*");
SysWatch.Created += new FileSystemEventHandler(SysWatch_Created);
SysWatch.EnableRaisingEvents = true;
PrintTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
PrintTimer.Start();
ErrorTimer.Elapsed += new System.Timers.ElapsedEventHandler(ErrorTimer_Elapsed);
}
private void SysWatch_Created(object Sender, FileSystemEventArgs w)
{
_filesToPrint.Add(w.FullPath);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_filesToPrint.Count > 0 && PrintTimerIsActive)
{
PrintTimer.Stop();
SendFilesToCom(_filesToPrint[0].ToString());
MessageBox.Show("Printing file :" + _filesToPrint[0].ToString());
File.Delete(_filesToPrint[0].ToString());
_filesToPrint.RemoveAt(0);
_filesToPrint.TrimToSize();
Console.Write(HsCOMerrorStrings);
PrintTimer.Start();
PrintErrors = ErrorCheck(HsCOMerrorStrings);
}
}
private void ErrorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs d)
{
if (!_serialPort1.IsOpen)
{
try
{
_serialPort1.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Data);
}
}
try
{
HsCOMerrorStrings = _serialPort1.ReadExisting();
_serialPort1.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Data);
}
Console.WriteLine(ErrorCheck(HsCOMerrorStrings));
}
private string ErrorCheck(string COMHostStatusReturn)
{
string ErrorsReturn = System.String.Empty;
string[] COMCode = COMHostStatusReturn.Split(',');
if (COMCode[1] == "1") { ErrorsReturn += "Carta Esaurita" + Environment.NewLine; }
if (COMCode[2] == "1") { ErrorsReturn += "Stampante in Pausa" + Environment.NewLine; }
if (COMCode[5] == "1") { ErrorsReturn += "Memoria Stampante Esaurita" + Environment.NewLine; }
if (COMCode[9] == "1") { ErrorsReturn += "Memoria RAM corrotta" + Environment.NewLine; }
if (COMCode[10] == "1") { ErrorsReturn += "Temperatura troppo bassa" + Environment.NewLine; }
if (COMCode[11] == "1") { ErrorsReturn += "Temperatura troppo alta" + Environment.NewLine; }
if (COMCode[7] == "1") { ErrorsReturn += "Partial Format Flag" + Environment.NewLine; }
if (ErrorsReturn != System.String.Empty)
{
if (PrintTimerIsActive == true)
{
PrintTimer.Stop();
if (!ErrorTimer.Enabled)
ErrorTimer.Start();
MessageBox.Show(ErrorsReturn);
}
PrintTimerIsActive = false;
}
else
{
if (PrintTimerIsActive == false)
{
ErrorTimer.Stop();
PrintTimer.Start();
PrintTimerIsActive = true;
}
}
return ErrorsReturn;
}
private void SendFilesToCom(string filePath)
{
if (!_serialPort1.IsOpen)
{
try
{
_serialPort1.Open();
}
catch (Exception ex)
{
}
}
try
{
using (System.IO.TextReader reader = File.OpenText(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
_serialPort1.WriteLine(line);
}
HsCOMerrorStrings = _serialPort1.ReadLine();
reader.Close();
_serialPort1.Close();
}
}
catch (Exception ex)
{
}
}
}
}
I made a small winforms application to monitor a certain folder for new pdf files, if a new pdf file is created in the particulair folder it will copy it to an other location.
The problem i'm having is that the filesystemwatcher creates double/multiple entries in my listbox, how can i solve this?
namespace Scanmonitor
{
public partial class Form1 : Form
{
FileSystemWatcher watcher = new FileSystemWatcher();
DateTime lastRead = DateTime.MinValue;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileWatch();
}
public void FileWatch()
{
watcher.Path = #"C:\Scanner";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
watcher.Filter = "*.pdf";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
public void OnChanged(object source, FileSystemEventArgs e)
{
scannerListBox.Items.Add(e.FullPath);
scannerListBox.SelectedIndex = scannerListBox.Items.Count - 1;
FileMove(scannerListBox.SelectedItem.ToString());
}
public void FileMove(string filePath)
{
try
{
System.IO.File.Copy(filePath, #"\\share\Data\Scans op OCE 600\" + Path.GetFileName(filePath));
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
}
}
}
}
Got it working.
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
FileInfo objFileInfo = new FileInfo(e.FullPath);
if (!objFileInfo.Exists) return;
System.Threading.Thread.Sleep(5000);
FileInfo fileinformatie = new FileInfo(e.FullPath);
string strCreateTime = fileinformatie.CreationTime.ToString();
string strCreateDate = fileinformatie.CreationTime.ToString();
strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));
ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
finally
{
watcher.EnableRaisingEvents = true;
}
}
You need to keep track of files (in a collection or dictionary) that already raised an event by the FileSystemWatcher. According to MSDN
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
FileInfo objFileInfo = new FileInfo(e.FullPath);
if (!objFileInfo.Exists) return;
System.Threading.Thread.Sleep(5000);
FileInfo fileinformatie = new FileInfo(e.FullPath);
string strCreateTime = fileinformatie.CreationTime.ToString();
string strCreateDate = fileinformatie.CreationTime.ToString();
//Ignore this, only for my file information.
strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));
ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
finally
{
watcher.EnableRaisingEvents = true;
}
}
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.