tags:

views:

54

answers:

4

I've got a simple method that does this:

private void searchButton_Click(object sender, EventArgs e)
    {
        searchResultsBox.Hide();
        doSomething();
    }

searchResultsBox is a listbox, and when I call its Hide method, it doesn't actually completely vanish until 'doSomething' finishes processing. It kind of leaves artifacts (in fact you can still see any part of the box that had an empty form surface behind it.

If I comment out 'doSomething', it vanishes promptly.

Any ideas on how to fix this? It's just a bit ugly.

A: 

Sound like doSomething is process intensive and blocking the GUI thread, not sure why it would not finishing hiding the list before executing doSomething.

I would try putting doSomething in to a separate thread.

benPearce
doSomething only takes a second or so to run, but it updates GUI controls so it can't be in another thread sadly. You're right though - it's weird behaviour.
ChristianLinnell
You could simply run it in another thread, thereby negating the need to update controls... or if you mean, changes them, then you can use Invoke() to pass control back to the GUI thread to change things.
Matthew Scharley
+4  A: 

You could try calling this.refresh() after calling searchResultsBox.Hide();

gcahill
Bingo. I couldn't find a Refresh method on the form object so I gave up looking. Thanks for that.
ChristianLinnell
No prob, as the guys pointed out below - refresh is useful for fixing a quick stall in the GUI thread but if doSomething() could take a lot of time, look into putting it into a separate thread.
gcahill
A: 

The separate thread or background worker process is the best answer. You can also try calling Application.DoEvents(). That seems to work at times for this specific issue although I'm not in favor of using that call often.

Cody C
+1  A: 

You should not do significant work in the GUI event thread, as it will cause the UI to freeze up while it is busy running your event handling code. It is a good practice to do any long-running tasks in another thread, either by signaling another already-running thread to do the work or by starting a new thread on the spot.

new Thread(new ThreadStart(doSomething)).Start();
John Kugelman
Agreed, but the behaviour is still detectable with as little as a 50ms wait time.
ChristianLinnell