8

I have a main UI thread which runs the application and creates the main window form (let's call it W). I have also a secondary thread that I spin up and which creates a dialog box (let's call it B).

I want to set the owner of the dialog B to be the main window W. The setting of Bs owner happens on the thread that created B. Basically:

b.Owner = w;

but this throws a cross-thread exception telling me that I am tryng to access the W object from the wrong thread.

So I tried to execute the code on the main UI thread, by using a Control.Invoke on W. But then, I get the same error telling me that I am trying to access B from the wrong thread:

System.InvalidOperationException was unhandled by user code
  Message=Cross-thread operation not valid: Control 'B' accessed from a
  thread other than the thread it was created on.
  Source=System.Windows.Forms

How am I supposed to do it right?

4
  • Don't use forms on multiple threads. It will give you more trouble than it's worth.
    – SLaks
    Mar 11, 2011 at 13:51
  • Did you read this SO post: stackoverflow.com/questions/3046245/… ? If this does not help you, you should post the code which gives you the error.
    – Doc Brown
    Mar 11, 2011 at 13:51
  • Perhaps you can tell us which feature(s) of ownership you were hoping to take advantage of (hopefully not all of them), and we could suggest ways to achieve similar effects? Mar 11, 2011 at 14:05
  • Thanks to all who posted; I know that I am asking for trouble. I need to run some legacy code on the main UI thread, and I want to have a progress dialog showing while this is happening; and I can't let the main UI thread handle the progress dialog. So I went for a progress dialog managed 100% by another thread. Mar 12, 2011 at 21:42

3 Answers 3

8

It's a bit of a bug in Winforms, Windows actually supports making the owner a window that was created on another thread. There's a way to disable that check, something you should never do. Except when you have to I suppose:

    private void button1_Click(object sender, EventArgs e) {
        var t = new Thread(() => {
            Control.CheckForIllegalCrossThreadCalls = false;
            var frm = new Form2();
            frm.Show(this);
            Control.CheckForIllegalCrossThreadCalls = true;
            Application.Run(frm);
        });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

I do not know if this is 100% safe, there could be a Winforms interaction that screws things up. You are in untested waters here, infested with threading sharks.

3
  • Your suggestion is interesting. In my case, I can make sure that the code which shows the dialog won't interfere with the main UI thread. I'll give it a try. Mar 12, 2011 at 21:45
  • 2
    Have you found out anything further about the safety of the above approach? I would like to do this (have one form own a form which is bound to a different thread) but see two concerns: (1) Showing a form which is owned by another will call Owner.AddOwnedForm which is probably not thread-safe; better to do the action on the owner form's thread; (2) If two forms bound to different threads try to show themselves simultaneously, one might set CheckForIllegalCrossThreadCalls=true between the time the other sets it false and the time it performs the operation that requires it to be false.
    – supercat
    Jun 19, 2013 at 18:25
  • Apparently another solution exists, where you pass the parent's .Handle to the thread's procedure as a parameter, and inside the procedure create a NativeWindow from that handle and pass it to .Show(). Then it is not required to switch off the CheckForIllegalCrossThreadCalls.
    – GSerg
    Dec 17, 2019 at 17:02
3

B needs to be created on the UI thread.

You can still interact with B from the secondary thread by using Control.Invoke.

1
  • In my case, it won't work for what I intend to do: my B window won't update if the main UI thread is blocked doing its work. Mar 12, 2011 at 21:43
2

If you're actually running two message loops on different threads, then there's no way to do what you're after. If you want W to own B, you're going to have to create B on the main thread and Invoke all of your interaction with B from the second thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.