tags:

views:

22

answers:

2

I have a form with a menu strip, a status strip, and a main split container (which itself contains other split containers, tree views, tab control, etc). Occasionally (fairly frequently, actually), the application will stop resizing controls within the main form (usually during or right after a background thread operation to retrieve data). The control itself still claims its DockStyle is Fill, but the control isn't resizing with the form. I can't find any information on what might cause this issue, has anyone come across anything like this before?

A: 

Making sure you manipulate the UI from the proper thread is critical. I recommend using the following extension methdds

public delegate void EmptyHandler();
public delegate void ParamHandler(params object[] args);

public static void SafeCall(this Control control, 
                      ParamHandler method, params object[] args)
{
    if (control.InvokeRequired)
    {
        control.Invoke(method, args);
    }
    else
    {
        method(args);
    }
}
public static void SafeCall(this Control control, EmptyHandler method)
{
    if (control.InvokeRequired)
    {
        control.Invoke(method);
    }
    else
    {
        method();
    }
}

then you can call the appropriate method from the incorrect thread like this

{  ... background thread 

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.SafeCall(SetImage, e.Result);
    }

 }

which calls the method SetImage(Bitmap bitmap) with the proper thread.

jalexiou
A: 

Well, that was stupid.

Turns out I had a rogue SuspendLayout() call that didn't have a corresponding ResumeLayout() call. This behaviour isn't what I would have expected. Learn something new every day! Thanks for looking!

genki