Event Handler not updating another class's forms

C

Clint

Hopefully I can explain the situation clearly. If not, I've got a project
that does.

I've created a C# application that sets up a FileSystemEventHandler event to
monitor a particular directory for the creation of a particular type of
file. At first glance, everything seems just hunky-dory. The event fires
when I want it to, and a message box was displayed.

So I thought, well, let's take it to the next level. Let's put in a funky
Outlook-style pop-up in the bottom left corner of the screen. So first off,
I created a separate class that contained the form, controls and code to do
what I wanted it to do. I dropped an extra button on my form to test the new
functionality, and it works great.

So now, I've got two pieces of code, both of them work fine separately.
There's only about 10 lines of actual code, and it should just be a matter
of copying the code from the preview button to the event handler of the
FileSystemEventHandler. Right? But now, when I do that, all that happens
is is the form appears, but the text boxes and colors aren't updated, and
when I move my mouse over the form, it just shows an hour-glass. Task
Manager shows minimal CPU activity, however. If I comment out my new code,
and just use the MessageBox again, it works fine.

I've also tried this with a form that's part of the same application, with
the same effect.

Here's the code I've got that uses the separate class:

private void btnStartMonitoring_Click(object sender, EventArgs e)
{
System.IO.FileSystemWatcher fsw = new
FileSystemWatcher(txtDirectoryName.Text.ToString());
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Filter = "*.cnk";
fsw.Created += new FileSystemEventHandler(OnCreated);
// Begin watching.
fsw.EnableRaisingEvents = true;
}

private static void OnCreated(object source, FileSystemEventArgs e)
{
string sMessage = "File: " + e.FullPath + " " + e.ChangeType;
//MessageBox.Show(sMessage); //if I un-comment this line, and comment
out the rest of this code, it works fine.

Notifier.Notifier notifier = new Notifier.Notifier();
notifier.DisplayNotification("This sucks"); //this function just updates
a text box on the notifier form.
}

private void btnShowMessage_Click(object sender, EventArgs e)
{
//This is my "Preview" button.
Notifier.Notifier notifier = new Notifier.Notifier();
notifier.DisplayNotification(txtMessage.Text.ToString());
}

Clint
 
N

nick_nw

Hi,

This is one of those things you want to try and see it happening for
yourself before you can really give super advise. That said I would
ask if you're using Invoke or BeginInvoke to make sure that any
accesses of a form's controls happen on that form's main thread.

If you want more details on this give me a shout.

Nick
 
C

Clint

Nick, I think you're right; thanks for that. I'm continuing to work through
this, but just wanted to let you know I saw your answer, and hopefully it
will get me going in the right direction. I'll try to post back when it's
done. Actually, when I get done the app I want to do, I'll probably try to
submit it to the CodeProject website.

Clint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top