G
G-Oker
Guest
Hi, I've searched around looking for what I'm trying to, and, although I think I've done it, it's not working.
I have a button that, when pressed, changes its title and listens for a file in a folder to change.
if the button is clicked again, I am trying (in vain ) to get FilesystemWatcher to stop listen for file changes.
private void Listen_Click(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
if (Listen.Text == "Listen")
{
Listen.Text = "Pause";
ModeBox.Clear();
ModeBox.BackColor = Color.Green;
ModeBox.ForeColor = Color.White;
ModeBox.AppendText("Listening Mode");
progressBar1.Visible = false;
CountBox.Clear();
looking.Visible = true;
CountBox.Visible = false;
//FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = FldPath;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "Test.xml";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
else if (Listen.Text == "Pause")
{
Listen.Text = "Listen";
ModeBox.Clear();
ModeBox.BackColor = Color.Red;
ModeBox.ForeColor = Color.White;
ModeBox.AppendText("Listening Mode PAUSED");
progressBar1.Visible = false;
CountBox.Clear();
looking.Visible = false;
CountBox.Visible = false;
// avoiding resource leak
///watcher.Changed -= OnChanged;
watcher.Path = null;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}
}
private void OnChanged(object source, FileSystemEventArgs e)
{
///display the file in another text box
}
The first section work (ie: it listen for files changes), but then I press the newly titled button (Pause), it still listens, and runs the OnChange procedure twice.
I am obviously missing something here, but I cannot for the life of me locate it.
Can someone point out what I'm done wrong, and what needs to be done to fix it ?
thank you
Continue reading...
I have a button that, when pressed, changes its title and listens for a file in a folder to change.
if the button is clicked again, I am trying (in vain ) to get FilesystemWatcher to stop listen for file changes.
private void Listen_Click(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
if (Listen.Text == "Listen")
{
Listen.Text = "Pause";
ModeBox.Clear();
ModeBox.BackColor = Color.Green;
ModeBox.ForeColor = Color.White;
ModeBox.AppendText("Listening Mode");
progressBar1.Visible = false;
CountBox.Clear();
looking.Visible = true;
CountBox.Visible = false;
//FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = FldPath;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "Test.xml";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
else if (Listen.Text == "Pause")
{
Listen.Text = "Listen";
ModeBox.Clear();
ModeBox.BackColor = Color.Red;
ModeBox.ForeColor = Color.White;
ModeBox.AppendText("Listening Mode PAUSED");
progressBar1.Visible = false;
CountBox.Clear();
looking.Visible = false;
CountBox.Visible = false;
// avoiding resource leak
///watcher.Changed -= OnChanged;
watcher.Path = null;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}
}
private void OnChanged(object source, FileSystemEventArgs e)
{
///display the file in another text box
}
The first section work (ie: it listen for files changes), but then I press the newly titled button (Pause), it still listens, and runs the OnChange procedure twice.
I am obviously missing something here, but I cannot for the life of me locate it.
Can someone point out what I'm done wrong, and what needs to be done to fix it ?
thank you
Continue reading...