I create a FileSystemWatcher on a separate thread to monitor a directory for changes. None of my events fire when I add a new file or copy a new file into the directory that I am trying to monitor. I've used the FileSystemWatcher class successfully in Windows Forms apps, so I am guessing I am missing something simple.
public partial class MainWindow : Window
{
System.IO.FileSystemWatcher watcher;
public MainWindow()
{
InitializeComponent();
System.Threading.Thread t1 = new System.Threading.Thread(MonitorDir);
t1.IsBackground = true;
t1.Start();
}
private void MonitorDir()
{
watcher = new System.IO.FileSystemWatcher("C:\\Temp","*.*");
watcher.Created += Watcher_Created;
watcher.Disposed += Watcher_Disposed;
watcher.Error += Watcher_Error;
watcher.Changed += Watcher_Changed;
while (true)
{
}
}
private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
throw new NotImplementedException();
}
private void Watcher_Error(object sender, System.IO.ErrorEventArgs e)
{
throw new NotImplementedException();
}
private void Watcher_Disposed(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
throw new NotImplementedException();
}
}
You need to set its EnableRaisingEvents property to true (it's false by default), otherwise it won't raise any events.
watcher.EnableRaisingEvents = true;
Related
I'm struggling with the AddingNew event on BindingList.
This is my code: the ListChanged is fired, the AddingNew not.
I'm missing something?
{
//....
System.ComponentModel.BindingList<string> test = new System.ComponentModel.BindingList<string>();
test.AllowNew = true;
test.RaiseListChangedEvents = true;
test.AddingNew += Test_AddingNew;
test.ListChanged += Test_ListChanged;
test.Add(new string("test1"));
test.Add("test2");
//....
}
private void Test_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
throw new NotImplementedException();
}
private void Test_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
throw new NotImplementedException();
}
I know this is old, but I was having the same issue. AddingNew is ONLY fired when BindingList.AddNew() is called. It is not called when BindingList.Add() is called.
Therefore either .ListChanged will need to be used or AddNew() will be required to fire the event.
Its working for me, but what is this? - new string("test1") <- remove it
class Program
{
static void Main(string[] args)
{
//....
System.ComponentModel.BindingList<string> test = new System.ComponentModel.BindingList<string>();
test.AllowNew = true;
test.RaiseListChangedEvents = true;
test.AddingNew += Test_AddingNew;
test.ListChanged += Test_ListChanged;
test.Add("test1");
test.Add("test2");
}
private static void Test_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
throw new NotImplementedException();
}
private static void Test_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
throw new NotImplementedException();
}
}
I am trying to monitor a log file for changes. My code is working, and does everything it should. However, as I want this to run as a windows service and be constantly monitoring I'm not sure of the proper way to set it into a waiting state. Here is what it's doing at the moment.
public static void Main()
{
log_watcher = new FileSystemWatcher();
log_watcher.Path = Path.GetDirectoryName(pathToFile);
log_watcher.Filter = recent_file.Name;
log_watcher.NotifyFilter = NotifyFilters.LastWrite;
log_watcher.Changed += new FileSystemEventHandler(OnChanged);
log_watcher.EnableRaisingEvents = true;
//do rest of stuff OnChanged
while (true)
{
}
}
And then just a simple:
public static void OnChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File has changed");
}
What would be a better way in a windows service to do this?
You can start a message pump using Application.Run() from WinForms.
using System.Windows.Forms;
// The class that handles the creation of the application windows
class MyApplicationContext : ApplicationContext {
private MyApplicationContext() {
// Handle the ApplicationExit event to know when the application is exiting.
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
log_watcher = new FileSystemWatcher();
log_watcher.Path = Path.GetDirectoryName(pathToFile);
log_watcher.Filter = recent_file.Name;
log_watcher.NotifyFilter = NotifyFilters.LastWrite;
log_watcher.Changed += new FileSystemEventHandler(OnChanged);
log_watcher.EnableRaisingEvents = true;
}
public static void OnChanged(object sender, FileSystemEventArgs e) {
Console.WriteLine("File has changed");
}
private void OnApplicationExit(object sender, EventArgs e) {
Console.WriteLine("File monitor exited.");
}
[STAThread]
static void Main(string[] args) {
// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.
MyApplicationContext context = new MyApplicationContext();
// Run the application with the specific context. It will exit when
// all forms are closed.
Application.Run(context);
}
}
See Run(ApplicationContext) on learn.microsoft.com.
Right I have the following code:
public partial class Form1 : Form
{
delegate void UpdateUI();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!Fiddler.CertMaker.rootCertExists())
{
if (!Fiddler.CertMaker.createRootCert())
{
throw new Exception("Unable to create cert for FiddlerCore.");
}
}
if (!Fiddler.CertMaker.rootCertIsTrusted())
{
if (!Fiddler.CertMaker.trustRootCert())
{
throw new Exception("Unable to install FiddlerCore's cert.");
}
}
Fiddler.FiddlerApplication.OnNotification += delegate (object snder, NotificationEventArgs oNEA) { MessageBox.Show("** NotifyUser: " + oNEA.NotifyString); };
Fiddler.FiddlerApplication.Log.OnLogString += delegate (object snder, LogEventArgs oLEA) { MessageBox.Show("** LogString: " + oLEA.LogString); };
Fiddler.FiddlerApplication.AfterSessionComplete += FiddlerApplication_OnAfterSessionComplete;
Fiddler.FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default & FiddlerCoreStartupFlags.DecryptSSL);
}
void FiddlerApplication_OnAfterSessionComplete(Session oSession)
{
if(oSession.fullUrl.Contains("google.com"))
richTextBox1.Invoke(new UpdateUI(() =>
{
richTextBox1.AppendText(oSession.GetResponseBodyAsString());
}));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Fiddler.FiddlerApplication.Shutdown();
}
}
The thing is with DecryptSSL flag set on startup the on aftersesssion complete never fires, I also never get any messages from either the notification or logs callbacks.
Any ideas?
I think port 0 might be a problem in the Startup(), I tried with port 8888:
Fiddler.FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default & FiddlerCoreStartupFlags.DecryptSSL);
I tried with these before response and before request handlers instead of OnAfterSessionComplete. In your Form1_Load():
Fiddler.FiddlerApplication.BeforeRequest += new SessionStateHandler(HandleBeforeRequest);
Fiddler.FiddlerApplication.BeforeResponse += new SessionStateHandler(HandleBeforeResponse);
And the handlers:
private void HandleBeforeRequest(Session oSession)
{
oSession.bBufferResponse = true;
}
private void HandleBeforeResponse(Session oSession)
{
if(oSession.fullUrl.Contains("google.com"))
{
richTextBox1.Invoke(new UpdateUI(() =>
{
richTextBox1.AppendText(oSession.GetResponseBodyAsString());
}));
}
}
By the way, don't know if you omitted them from your sample but I needed to add these in the constructor:
Load += Form1_Load;
FormClosing += Form1_FormClosing;
Might also be good to add this before Shutdown():
FiddlerApplication.oProxy.Detach();
I am learning some WPF and have written this little program that reads an Excel file for data and updates the UI on save. Only after the first save does my ResetTimer() function work. But the GetDisplayData() does load the data and the program will update data on save. Only they timer does not start until that first save..
But I want the timer to start right away in case there is not a save event on the Excel file at load.
What can I do to get it to work, seems like whenever I try and place it in window_loaded or other places I tried, my program loops or does not load the data.
Thank you for your help.
using System;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Threading;
namespace WPFReadExcel
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string ExcelPath = #"C:\";
private const string ExcelPathFile = #"C:\DataSource.xlsx";
DataTable _dashBoardData = new DataTable();
public MainWindow()
{
InitializeComponent();
}
protected void Window_Loaded(object sender, RoutedEventArgs e)
{
GetDisplayData();
StartFileSystemWatcher();
}
public void GetDisplayData()
{
var excelData = new ExcelData();
_dashBoardData = excelData.ReadExcelFile("Live", ExcelPathFile);
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ExcelDataGrid.ItemsSource = _dashBoardData.AsDataView();
RefreshDateTime.Content = "Refresh at: " +
DateTime.Now.ToShortTimeString();
}
));
}
private void ResetDisplayData()
{
if (_dashBoardData != null) _dashBoardData.Dispose();
GetDisplayData();
ResetTimer();
}
private void ResetTimer()
{
while (true)
{
System.Threading.Thread.Sleep(20000);
ResetDisplayData();
}
}
private void StartFileSystemWatcher()
{
if (string.IsNullOrWhiteSpace(ExcelPath))
return;
FileSystemWatcher watcher = new FileSystemWatcher();
// set directory to watch
watcher.Path = ExcelPath;
// set what to watch for
watcher.NotifyFilter = NotifyFilters.LastWrite;
// set event handlers
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
// start watching
watcher.EnableRaisingEvents = true;
}
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
ResetDisplayData();
}
private void Label_Loaded(object sender, RoutedEventArgs e)
{
RefreshDateTime.Content = "Refresh at: " + DateTime.Now.ToShortTimeString();
}
}
}
The Window.Loaded event is the correct place to do what you want:
protected void Window_Loaded(object sender, RoutedEventArgs e)
{
ResetTimer();
GetDisplayData();
StartFileSystemWatcher();
}
However, it appears that you aren't using a Timer anywhere, so your question and your method name are inappropriate. In WPF, we use the DispatcherTimer Class. First, you'd need to initialise it and then start it:
private DispatcherTimer timer = new DispatcherTimer();
...
private void ResetTimer()
{
timer.Interval = TimeSpan.FromSeconds(20);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
ResetDisplayData();
}
For your information, you really couldn't write much worse code than this, as it will block your UI and make your application non responsive:
while (true)
{
System.Threading.Thread.Sleep(20000);
ResetDisplayData();
}
How can I increment a value per second, when I passed it from another page?
Here is some code , where I get the value from the previous page + I added the Timer.
The Problem is that the EventHandler that has to been created for the Tick, can t be set to OnNavigatedTo.
public partial class Page1 : PhoneApplicationPage
{
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(.1) };
public Page1()
{
InitializeComponent();
this.timer.Tick += new EventHandler(OnNavigatedTo);
this.Loaded += new RoutedEventHandler(OnNavigatedTo);
}
private void ButtonToPage1(object sender, RoutedEventArgs e)
{
App app = Application.Current as App;
MessageBox.Show(app.storeValue);
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string QueryStr = "";
NavigationContext.QueryString.TryGetValue("myNumber", out QueryStr);
int test = (int.Parse(QueryStr));
}
try this:
DispatcherTimer tmr;
int test;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string QueryStr = "";
NavigationContext.QueryString.TryGetValue("myNumber", out QueryStr);
test = (int.Parse(QueryStr));
LoadTimer();
}
public void LoadTimer()
{
tmr = new DispatcherTimer();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
tmr.Interval = new TimeSpan(0, 0, 1);
tmr.Tick += tmr_Tick;
tmr.Start();
});
}
void tmr_Tick(object sender, EventArgs e)
{
test++;
TextBlock.Text = test.ToString();
}
It isn't clear why you can't just follow the tutorial linked in your comment. I guess you misunderstand it and tried to handle Tick event using OnNavigatedTo() method. Yes, that won't work and you aren't supposed to do so.
You're supposed to simply attach event handler method in OnNavigatedTo :
private int myNumber;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string QueryStr = "";
NavigationContext.QueryString.TryGetValue("myNumber", out QueryStr);
myNumber = (int.Parse(QueryStr));
DispatcherTimer newTimer = new DispatcherTimer();
newTimer.Interval = TimeSpan.FromSeconds(1);
//attach event handler method for Tick event
newTimer.Tick += OnTimerTick;
//or attach anonymous method so you don't need OnTimerTick() method :
//newTimer.Tick += (o, e) => { myNumber++; };
newTimer.Start();
}
void OnTimerTick(Object sender, EventArgs args)
{
myNumber++;
}