FileSystemWatcher & Modal Dialog Box

G

Guest

I'm trying to build a windows forms application that monitors changes in the
file system using a FileSystemWatcher

object. In the event handler of the watcher's 'Changed' event, i would like
to display a message box. If i do this,

the messagebox is shown, but it is not displayed modal!

If i replace the code 'MessageBox.Show( "Hi there!" );' with 'new
Form().ShowDialog();', a modal form is shown, but

this form does not behave 100% as a normal modal form (e.g. the title bar
does not blink if i click the main form)

I tried to set the SynchronizingObject property of the watcher (which makes
the this.InvokeRequired test obsolete),

but this did not change the behaviour.

As a reference, i've also tried to launch modal forms from a
System.Threading.Thread threadstart delegate and from a

threadpool worker callback, but this all worked fine...

What's so special about the FileSystemWatcher callback thread that it
prevents forms from showing themselves as modal

forms?

Any ideas?

Some code snippets can be found below:

public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private FileSystemWatcher m_fileSystemWatcher;

public Form1()
{
InitializeComponent();
m_fileSystemWatcher = new FileSystemWatcher();
m_fileSystemWatcher.Filter = "*.txt";
m_fileSystemWatcher.Path = @"C:\";
m_fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
m_fileSystemWatcher.EnableRaisingEvents = true;
m_fileSystemWatcher.Changed += new
FileSystemEventHandler(m_fileSystemWatcher_Changed);
}
...
}

private void m_fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
ShowMessageBox();
}


private void ShowMessageBox()
{
if( this.InvokeRequired )
{
MethodInvoker invoker = new MethodInvoker( ShowMessageBox );
this.Invoke( invoker );
return;
}
MessageBox.Show( "Hi there!" );
}
 
C

CT

You're nearly there, just add the owner in teh call to MessageBox.Show, like
this:

MessageBox.Show(this, "Hi there!" );
 

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