I'm trying to watch the directory where my application .exe runs from. Watching for directory changes, filename changes, etc etc
I'm using the FileSystemWatcher class and set the Path property to the right directory just fine. When I ran my original application, i used just a regular method for handling the Changed event and tried updating my textBox1.Text property to show the Name property however I was receiving the cross thread exception that shows that your not supposed to do it that way unless you want possible instability....
So I read the MSDN article about how to make thread-safe calls to update GUI controls. There are two known methods for doing so as shown in the MSDN article. http://msdn.microsoft.com/en-us/library/ms171728.aspx
1. Invoke
2. BackgroundWorker
After reading the Invoke example, I get lost at the part where it says this....
As shown above, it's implying we know what the SetTextCallback(SetText) method is supposed to do and how it's implemented but since i'm new, i have no idea what this is about....
Instead, I chose to try the BackgroundWorker example instead so after implementing the BackgroundWorker method, I end up receiving the very same cross thread exception as originally when I tried to do it just by trying to set the Text property in the Changed event handler.
So i've implemented the BackgroundWorker method for making the aparently thread-safe update to my TextBoxes Text propertly as shown on the MSDN site, but I still receive the cross thread exception error!
Any help,
Much appreciated,
I'm using the FileSystemWatcher class and set the Path property to the right directory just fine. When I ran my original application, i used just a regular method for handling the Changed event and tried updating my textBox1.Text property to show the Name property however I was receiving the cross thread exception that shows that your not supposed to do it that way unless you want possible instability....
So I read the MSDN article about how to make thread-safe calls to update GUI controls. There are two known methods for doing so as shown in the MSDN article. http://msdn.microsoft.com/en-us/library/ms171728.aspx
1. Invoke
2. BackgroundWorker
After reading the Invoke example, I get lost at the part where it says this....
Code:
SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text });
Instead, I chose to try the BackgroundWorker example instead so after implementing the BackgroundWorker method, I end up receiving the very same cross thread exception as originally when I tried to do it just by trying to set the Text property in the Changed event handler.
So i've implemented the BackgroundWorker method for making the aparently thread-safe update to my TextBoxes Text propertly as shown on the MSDN site, but I still receive the cross thread exception error!
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace FileSystemWatcherTest1
{
public partial class Form1 : Form
{
BackgroundWorker myWorker1 = new BackgroundWorker();
FileSystemWatcher myWatcher = new FileSystemWatcher();
Regex r = new Regex(@".*\..*");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
// Set methods for Background worker's DoWork and RunWorkerCompleted events.
myWorker1.DoWork += new DoWorkEventHandler(myWorker1_DoWork);
myWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(myWorker1_RunWorkerCompleted);
// Get this applications path.
string path = Application.ExecutablePath.ToString();
string[] results = path.Split(new char[] { '\\' }, StringSplitOptions.None);
int num = results.Length - 1;
results.SetValue("", num);
string finalPath = String.Join("\\", results);
// set the Path property
myWatcher.Path = finalPath;
myWatcher.IncludeSubdirectories = true;
myWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.LastWrite
| NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.Size;
// add even handlers
myWatcher.Changed += new FileSystemEventHandler(myWatcher_Changed);
// Enable raising events
myWatcher.EnableRaisingEvents = true;
//textBox1.Text = "Path has been set on the FileSystemWatcher";
}
void myWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// update the UI control here, still gets a cross thread exception...
this.textBox1.Text += (string)e.Result + Environment.NewLine;
}
void myWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgworker = sender as BackgroundWorker;
e.Result = (string)e.Argument;
}
void myWatcher_Changed(object sender, FileSystemEventArgs e)
{
myWorker1.RunWorkerAsync(e.Name);
}
}
}
Much appreciated,