I'm writing a program that has multiple event handlers in it, new territory for me. One event handler watches a folder and when a file is moved into it it opens the file, gets one piece of information out of the file, stores that data in a list of objects and then moves the file to another folder. The second event handler is based off of a timer where it's interval is set for 10 seconds. When the timer elapses it loops through the list I've loaded up and checks to see if the timestamp of when the item was loaded to the list is more than 1 minute old and if it is then it just displays a message to the console. I have two main problems: first) the message that should be displayed to the console once a minute is displaying more often than it should (about once every 15 seconds as opposed to once a minute) and 2) when I try to debug the program and it gets to the timer event handler it jumps all over the place that doesn't follow any logical path to me (I'm probably wrong). Please, can someone help me out? Any help would be much appreciated.
Code:
public class fileObject
{
public string strPONumber { get; set; }
public DateTime dtArrival { get; set; }
}
private static System.Timers.Timer aTimer;
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
Console.WriteLine("redefined");
List<fileObject> lstPONums = new List<fileObject>();
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = #"C:\Users\z088476\Desktop\FolderA\";
// Only watch text files.
watcher.Filter = "*";
// Add event handlers.
Console.WriteLine("before event");
watcher.Created += new FileSystemEventHandler((sender, e) => OnChanged(sender, e, lstPONums));
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler((sender, e) => OnTimedEvent(sender, e, lstPONums));
aTimer.Interval=10000;
aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e, List<fileObject> lstPONumbers)
{
fileObject fObj = new fileObject();
string strLine = "";
using (StreamReader sr = new StreamReader(e.FullPath))
{
strLine = sr.ReadLine();
}
string strDelim = strLine.Substring(3,1);
List<string> lstSplit = new List<string>();
lstSplit = strLine.Split(Convert.ToChar(strDelim)).ToList();
fObj.strPONumber = lstSplit[30];
fObj.dtArrival = DateTime.Now;
lstPONumbers.Add(fObj);
File.Move(e.FullPath, #"C:\Users\z088476\Desktop\FolderC\" + e.Name);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e, List<fileObject> lstPONumbers)
{
if (lstPONumbers.Count != 0)
{
foreach (fileObject fo in lstPONumbers)
{
DateTime dtnow = DateTime.Now;
TimeSpan duration = DateTime.Now - fo.dtArrival;
if (duration.TotalMinutes > 1)
{
Console.WriteLine("IT IS MORE THAN 1 MIN!!!!");
}
}
}
}
Related
In wpf, I have a textbox, the content of it coming from a txt file.
in code behine it looks like:
public MyWindow()
{
InitializeComponent();
ReadFromTxt();
}
void Refresh(object sender, RoutedEventArgs e)
{
ReadFromTxt();
}
void ReadFromTxt()
{
string[] lines = System.IO.File.ReadAllLines(#"D:\Log.txt");
foreach (string line in lines)
{
MyTextBox.AppendText(line + Environment.NewLine);
}
}
The thing is that the txt file content is changing during the runtime, is it possible to be synchronized with those changes? I mean that the textbox content would be change if the txt file is changed. if It's possible please give some code example i c# or XAML.
My solution for now is that I made a "Refresh" button that reads again from the txt file
You can configure FileSystemWatcher. I added FileWatcherConfigure function as an example.
Whenever file changes, FileChanged event will be published. You should trigger the RefreshTextbox method with that event.
FileSystemWatcher fileWatcher = new FileSystemWatcher();
string filePath = #"./Log.txt";
public MainWindow()
{
InitializeComponent();
ReadFromTxt();
FileWatherConfigure();
}
public void FileWatherConfigure()
{
fileWatcher.Path = System.IO.Path.GetDirectoryName(filePath);
fileWatcher.Filter = System.IO.Path.GetFileName(filePath);
fileWatcher.Changed += FileWatcher_Changed;
fileWatcher.EnableRaisingEvents = true;
}
private void FileWatcher_Changed(object sender, FileSystemEventArgs e)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
ReadFromTxt();
}
void ReadFromTxt()
{
string[] lines = System.IO.File.ReadAllLines(filePath);
MyTextBox.Dispatcher.Invoke(() => { this.MyTextBox.Text = string.Join(Environment.NewLine, lines); });
}
You need a FileSytemWatcher that will trigger an event that you can use to refresh the content of your textbox automatically.
I am using the following in a project I am working on to detect new and updated file.
I watch for the:
LastAccess: To get an event when the file has been changed
LastWrite: To get an event when the file has been changed
FileName: To get an event when a file is renamed that would match
Please note that I am clearing the textbox each time to avoid duplicated lines (your code would append the whole file to the existing content)
public class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = #"D:\";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "Log.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnFileChanged);
watcher.Created += new FileSystemEventHandler(OnFileChanged);
watcher.Renamed += new RenamedEventHandler(OnFileRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
ReadFromTxt();
}
private void OnFileChanged(object source, FileSystemEventArgs e)
{
ReadFromTxt();
}
private void OnFileRenamed(object source, RenamedEventArgs e)
{
ReadFromTxt();
}
private void ReadFromTxt()
{
// The easiest way is to just replace the whole text
MyTextBox.Text = "";
if (System.IO.File.Exists(#"D:\Log.txt") == false)
{
return;
}
string[] lines = System.IO.File.ReadAllLines(#"D:\Log.txt");
foreach (string line in lines)
{
MyTextBox.AppendText(line + Environment.NewLine);
}
}
}
Just to clarify on other answers, FileSystemWatcher can be an option but you should definately consider polling as an alternative. For readability (opinion-based) and reliability (see this thread). Note that there is many other posts that underline issues concerning the use of FileSystemWatcher.
And by polling I mean look for changes on specific periodic periods. An option would be to use a Timer and look for changes and act accordingly.
View :
<TextBox Text="{Binding myText}"/>
Model :
private string _myText;
public string MyText{
get { return _myText; }
set { _myText = value;
NotifyPropertyChanged();
}
Implement INotifyPropertyChangedto your Window
And change the property MyText and not MyTextBox
I have been following the guidance here FileSystemWatcher Changed event is raised twice.
However I have a list of files that I'm watching so if I delete 20 files together the event is called 20 times. This is expected and works fine.
How can I only have an event fired once for 20 "simultaneous" file changes (i.e How can I ignore all other file changes until the code in the first instance of Onchanged below has completed. Right now Onchanged is called 20 times.) ?
private void Main_Load(object sender, EventArgs e)
{
public static List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
UpdateWatcher();
}
public void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
Logging.Write_To_Log_File("Item change detected " + e.ChangeType + " " + e.FullPath + " " + e.Name, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);
watchers.Clear();
foreach (FileSystemWatcher element in MyGlobals.watchers)
{
element.EnableRaisingEvents = false;
}
//Do some processing on my list of files here....
return;
}
catch (Exception ex)
{
// If exception happens, it will be returned here
}
finally
{
foreach (FileSystemWatcher element in MyGlobals.watchers)
{
element.EnableRaisingEvents = true;
}
}
}
public void UpdateWatcher() // Check Items
{
try
{
watchers.Clear();
for (int i = 0; i < MyGlobals.ListOfItemsToControl.Count; i++) // Loop through List with for
{
FileSystemWatcher w = new FileSystemWatcher();
w.Path = Path.GetDirectoryName(MyGlobals.ListOfItemsToControl[i].sItemName); // File path
w.Filter = Path.GetFileName(MyGlobals.ListOfItemsToControl[i].sItemName); // File name
w.Changed += new FileSystemEventHandler(OnChanged);
w.Deleted += new FileSystemEventHandler(OnChanged);
w.Created += new FileSystemEventHandler(OnChanged);
w.EnableRaisingEvents = true;
watchers.Add(w);
}
}
catch (Exception ex)
{
// If exception happens, it will be returned here
}
}
The key point here is what does "together" mean to you. after all the system does an independent delete operation for each, which would technically mean they are not all deleted at the exact same time, but if you just wanna be close, let's say if they are all deleted within 5 seconds of each other then we only want OnChange to fire once, you can do the following. Note that this doesn't handle the rename change notification. You weren't listening for it, so I assumed you don't need to.
you may wanna change the 5 seconds window to a small window depending on your use.
class SlowFileSystemWatcher : FileSystemWatcher
{
public delegate void SlowFileSystemWatcherEventHandler(object sender, FileSystemEventArgs args);
public event SlowFileSystemWatcherEventHandler SlowChanged;
public DateTime LastFired { get; private set; }
public SlowFileSystemWatcher(string path)
: base(path)
{
Changed += HandleChange;
Created += HandleChange;
Deleted += HandleChange;
LastFired = DateTime.MinValue;
}
private void SlowGeneralChange(object sender, FileSystemEventArgs args)
{
if (LastFired.Add(TimeSpan.FromSeconds(5)) < DateTime.UtcNow)
{
SlowChanged.Invoke(sender, args);
LastFired = DateTime.UtcNow;
}
}
private void HandleChange(object sender, FileSystemEventArgs args)
{
SlowGeneralChange(sender, args);
}
protected override void Dispose(bool disposing)
{
SlowChanged = null;
base.Dispose(disposing);
}
}
This is mt first time trying to write a not web based program, and my first time writing anything in C#.
I need a program that monitors folders, but I can't get it to work.
I have used the example from this post Using FileSystemWatcher with multiple files but is trying to make it a form.
My current problem comes in the ProcessQueue function where fileList apparently is defined in another thread.
Whenever a file is actually submitted to the watched folder I get an error that using fileList is a cross thread call
Can anyone explain this error to me, and how to fix it?
namespace matasWatch
{
public partial class Form1 : Form
{
private int n = 1;
private bool isWatching = false;
private List<string> filePaths;
private System.Timers.Timer processTimer;
private string watchedPath;
private FileSystemWatcher watcher;
public Form1()
{
filePaths = new List<string>();
watchedPath = "C:\\Users\\username\\Desktop\\test";
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!isWatching)
{
button1.Text = "Stop";
isWatching = true;
watcher = new FileSystemWatcher();
watcher.Filter = "*.*";
watcher.Created += Watcher_FileCreated;
watcher.Error += Watcher_Error;
watcher.Path = watchedPath;
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
}
else {
button1.Text = "Watch";
isWatching = false;
watcher.EnableRaisingEvents = false;
watcher.Dispose();
watcher = null;
}
}
private void Watcher_Error(object sender, ErrorEventArgs e)
{
// Watcher crashed. Re-init.
isWatching = false;
button1_Click(sender, e);
}
private void Watcher_FileCreated(object sender, FileSystemEventArgs e)
{
filePaths.Add(e.FullPath);
if (processTimer == null)
{
// First file, start timer.
processTimer = new System.Timers.Timer(2000);
processTimer.Elapsed += ProcessQueue;
processTimer.Start();
}
else{
// Subsequent file, reset timer.
processTimer.Stop();
processTimer.Start();
}
}
private void ProcessQueue(object sender, ElapsedEventArgs args)
{
try
{
fileList.BeginUpdate();
foreach (string filePath in filePaths)
{
fileList.Items.Add("Blaa");
}
fileList.EndUpdate();
filePaths.Clear();
}
finally
{
if (processTimer != null)
{
processTimer.Stop();
processTimer.Dispose();
processTimer = null;
}
}
}
}
}
I assume that fileList is a windows forms control. The ProcessQueue method is called from a timer thread which is by default a background thread. The fileList control resides in the UI thread. You need to use the Invoke() method of the form passing it in a delegate the updates the fileList control.
Invoke(new Action(() =>
{
fileList.BeginUpdate();
foreach (string filePath in filePaths)
{
fileList.Items.Add("Blaa");
}
fileList.EndUpdate();
filePaths.Clear();
}));
Try using System.Windows.Forms.Timer instead of System.Timers.Timer so the timer tick event is executed on the UI thread.
See here for more details.
I'm creating a List of FileSystemWatchers.
List<ExtSystemFileWatcher> fswMonitors = new List<FileSystemWatcher> ();
The number of them in the list is dynamic depending on the user. This is done from a INI file and an array of Monitor objects from my own Monitor class are created. The class simply has varibles like the Montior number, Path to monitor, Ext to look for etc.
if (iNumberMonitors > 0)
{
obMonitors = ReadMonitors(iNumberMonitors);
for (int iCounter = 0; iCounter < iNumberMonitors; iCounter++)
{
FileSystemWatcher fswCurrent = new FileSystemWatcher();
fswCurrent.Path = obMonitors[iCounter].strMonPath;
fswCurrent.EnableRaisingEvents = true;
fswCurrent.NotifyFilter = NotifyFilters.FileName;
fswCurrent.Filter = "*." + obMonitors[iCounter].strMonExt;
fswCurrent.Deleted += OnDelete;
fswMonitors.Add(fswCurrent);
}
}
In the 'OnDelete' Method that each FileSystemWatcher calls if the Delete event fires I need to know which of the FileSystemWatchers is calling it.
My question is how can I know which FileSystemMonitor in the List is calling the method?
Do you need anything else more than just checking sender in your eventHandler?
private void OnDelete(object sender, ...)
{
var watcher = (FileSystemWatcher) sender;
// probably list.IndexOf here if you really need an index
}
You can use a closure where you hook your "Delete event"
instead of passing the method itself:
fswCurrent.Deleted += OnDelete;
you pass something like
fswCurrent.Deleted += (sender, e) => OnDelete(sender, e, iCounter)
of course you will need to change the signature of OnDelete to take the additional Index.
After reading your comment you might don't need it though, as the other answers suggests.
The event handler has a sender parameter that is a reference to the FileSystemWatcher that raised the event.
private static void OnDeleted(object source, FileSystemEventArgs e)
{
FileSystemWatcher watcher = source as FileSystemWatcher;
if(watcher != null)
{
string deletedFile = e.FullPath;
//Update db with watcher and deletedFile
}
}
"sender" parameter in OnDelete method will point to the originator of the event
void OnDelete(object sender, EventArgs e)
{
var watcher = ((FileSystemWatcher) sender);
.....
}
Just cast the sender parameter of the event handler which you attach to the Deleted event.
For example:
class Program
{
static void Main(string[] args)
{
var watcher = new FileSystemWatcher { Path = #"c:\temp", Filter = "*.txt" };
watcher.Deleted += watcher_Deleted;
watcher.EnableRaisingEvents = true;
Console.ReadLine();
}
static void watcher_Deleted(object sender, FileSystemEventArgs e)
{
var watcher = sender as FileSystemWatcher;
}
}
May be check if each of these pooled objects might have an hashcode in it. Which should be unique.
this is my first C# post.
I have a question on event binding.
I have a FileWatcher which I'd like to bind to functions that are defined in a separate class called FileWatcherEvents.
I don't want the events to be declared in the Program class, how can this be done?
As you can see, I try to bind the events for Created and Deleted.
The problem is that these events are not called when I delete or create a file in the folder. But when I declare the event handlers in the Program class, it does work.
Any help or insights is appreciated.
Program
using System.IO;
class Program : ServiceBase
{
private FileSystemWatcher _watcher;
public Program()
{
FileWatcherEvents fwe = new FileWatcherEvents();
this._watcher = new FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this._watcher)).BeginInit();
//
// _watcher
//
this._watcher.EnableRaisingEvents = true;
this._watcher.Filter = "*.txt";
this._watcher.NotifyFilter =
((NotifyFilters)(((((NotifyFilters.FileName
| NotifyFilters.DirectoryName)
| NotifyFilters.LastWrite)
| NotifyFilters.LastAccess)
| NotifyFilters.CreationTime)));
this._watcher.Path = "T:\\out\\";
this._watcher.Deleted += new FileSystemEventHandler(fwe.ShipmentFileCreated);
this._watcher.Created += new FileSystemEventHandler(fwe.FileDeleted);
((System.ComponentModel.ISupportInitialize)(this._watcher)).EndInit();
}
static void Main(string[] args)
{
Program prg = new Program();
Console.Write(FileManager.getNewestFile("T:\\out\\"));
while (Console.Read() != 'q') ;
}
}
FileWatcherEvents
class FileWatcherEvents
{
public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
}
public void FileDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
}
}
I believe you would need to declare fwe in a larger scope, like at the Program level instead of inside the Program constructor. Otherwise the object will go away, and possibly all the events that lead to it as well (never been entirely clear on what happens to the functions that handle events on an instance when the instance goes away, but the events could still occur, but it's very possible they will no longer run).
Edit:
I got your code to work with some minor adjustments. Mainly I had to move EnableRaisingEvents to the end of the block because .NET throws an exception if you do it before setting the path. How did you not see that exception?
class Program
{
private FileSystemWatcher _watcher;
public Program()
{
FileWatcherEvents fwe = new FileWatcherEvents();
this._watcher = new System.IO.FileSystemWatcher();
this._watcher.Filter = "*.txt";
this._watcher.NotifyFilter = ((System.IO.NotifyFilters)(((((
System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)
| System.IO.NotifyFilters.LastWrite) | System.IO.NotifyFilters.LastAccess)
| System.IO.NotifyFilters.CreationTime)));
this._watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
this._watcher.Deleted += new System.IO.FileSystemEventHandler(fwe.ShipmentFileCreated);
this._watcher.Created += new System.IO.FileSystemEventHandler(fwe.FileDeleted);
this._watcher.EnableRaisingEvents = true;
Console.ReadLine();
}
public static void Main()
{
Program prg = new Program();
using (System.IO.StreamWriter w = new System.IO.StreamWriter(
System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TestFile.txt"), false))
{
w.WriteLine(DateTime.Now.ToLongTimeString());
}
Console.ReadLine();
}
}
class FileWatcherEvents
{
public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
}
public void FileDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
}
}
Turns out that the code works fine, the events fired, but the functions weren't because of the *.txt filter in the private FileSystemWatcher object.