Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form.Show freezes if called within event handler

Status
Not open for further replies.

courtarro

Programmer
Oct 26, 2005
16
US
I have a problem with an event handler that opens a form. The form appears, but it freezes immediately. This doesn't happen for all events, such as those that my other forms create (like menu items or button clicks), but it does happen for the system-based events like FileSystemWatcher and NetworkAddressChanged.

Here's what I did to demonstrate the problem:

I've made an event handler for the FileSystemWatcher.Created event, so that it is fired whenever someone creates a file in the monitored directory:

FileSystemWatcher watcher = new FileSystemWatcher("c:\\");
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;

In that handler, I create an instance of a very simple form that has no code other than the designer stuff:

static void watcher_Created(Object sender, FileSystemEventArgs e) {
Form test = new Form1();
test.Show();
}

When I run my program, I go into c:\ and create a new file, causing the event to fire. The form pops up, but it is completely frozen. You can't close it, and the button isn't visible. The cursor is an hourglass. When I run this code in my real app, it's clear that the program is still functioning in the background because other forms still respond. However, the form that was opened by the event is completely unresponsive. Does anyone know why this might happen? I have a very simple test solution that I can link to if necessary.
 
I may be wrong here, but the FileSystemWatcher may be firing that event on a different thread :)

This means that when you launch your GUI item it responds on that thread only. One way around this would be to invoke a method on your main form to spawn the new form.

private void OpenAForm(object sender, EventArgs e)
{
Form1 test = new Form1();

test.Show();
}

static void watcher_Created(Object sender, FileSystemEventArgs e)
{
this.Invoke(new EventHandler(OpenAForm), new object[] {this, EventArgs.Empty});
}
 
That was not exactly what I needed, but it got me on the right track. The problem was indeed that the form had started but that the parent thread (started by the event) was ending as soon as the form was displayed. Thus, there was no thread available to handle messages for that form.

The problem is that my app has no main form, so there is no such thread available for using Invoke(). Instead, to fix it, I simply started another application message loop, replacing:

Code:
Form test = new Form1();
form.Show();

with:

Code:
Form test = new Form1();
Application.Run(test);

This starts the new form and keeps the thread alive to process form messages, just like the application would do for a main form if one existed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top