views:

167

answers:

1

I am trying to access a form from a different thread to that on which the form was created, and finally ended up with an error: Cross thread operation not valid

public static void MakeTopMost(Form form)
{
    SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
}

I am passing a form which is running in another thread. I tried testing InvokeRequired, but it is always false.

I am new to threading.

+8  A: 

Make sure you are testing the right object for InvokeRequired:

public static void MakeTopMost(Form form)
{
    if (form.InvokeRequired)
    {
        form.Invoke((Action)delegate { MakeTopMost(form); });
        return;
    }

    SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
}

I like to wrap all of this up with an extension method like this:

public static class SynchronizeInvokeUtil
{
    public static void SafeInvoke(this ISynchroniseInvoke sync, Action action)
    {
        if (sync.InvokeRequired)
            sync.Invoke(action);
        else
            action();
    }

    public static void SafeBeginInvoke(this ISynchroniseInvoke sync, 
                                       Action action)
    {
        if (sync.InvokeRequired)
            sync.BeginInvoke(action);
        else
            action();
    }
}

You can then just call:

form.SafeInvoke(() => SetWindowPos(form.Handle, HWND_TOPMOST, 
                                   0, 0, 0, 0, TOPMOST_FLAGS));

Which is probably the most readable.

Note that if you are using this within the form class itself, you have to use this.SafeInvoke(...) in order to access the extension method.

Drew Noakes
@Drew Noakes, now there is no error in cross threading operation, but still no fruits. Its not getting my form to front.
Anuya
@karthik - if you can't get the form to the front, have another search on SO (I'm sure there are lots of questions about that on here). If that doesn't help, ask a new question.
Drew Noakes