0

I am debugging a WinForm control and it returns InvokeRequired = true although i am on Main thread. Is it possible for a control to be child of a non-UI thread? I thought control which is an UI component can only be child of UI thread.

user156144
  • 2,215
  • 4
  • 29
  • 40

1 Answers1

0

Yes, it is possible, that control was created on some other thread than application (ui) thread, please see example bellow.

        var task = Task<Button>.Factory.StartNew(() => {
            var button = new Button();
            var thisForcesControlToCreateHandle = button.Handle;
            return button;
        });
        var taskResult = task.Result;
        var invokeRequired = taskResult.InvokeRequired; // will be True

It is important to read control property Handle, that forces handle creation, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx form more info.

SetyCZ
  • 150
  • 7