Home All Groups Group Topic Archive Search About

FileSystemWatcher & Modal Dialog Box

Author
13 Oct 2005 7:00 AM
Pros Van Dessel
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!" );
}

Author
13 Oct 2005 7:24 AM
CT
You're nearly there, just add the owner in teh call to MessageBox.Show, like
this:

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


--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

Show quote
"Pros Van Dessel" <Pros Van Des***@discussions.microsoft.com> wrote in
message news:F8932ACF-D6AB-460E-AC54-2FA5F27BA9BD@microsoft.com...
> 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!" );
> }
Author
13 Oct 2005 7:41 AM
Pros Van Dessel
Thanks!  Now it works...

Show quote
"CT" wrote:

> You're nearly there, just add the owner in teh call to MessageBox.Show, like
> this:
>
> MessageBox.Show(this, "Hi there!" );
>
>
> --
> Carsten Thomsen
> Communities - http://community.integratedsolutions.dk
>
> "Pros Van Dessel" <Pros Van Des***@discussions.microsoft.com> wrote in
> message news:F8932ACF-D6AB-460E-AC54-2FA5F27BA9BD@microsoft.com...
> > 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!" );
> > }
>
>
>

AddThis Social Bookmark Button