Possible to construct form on background thread, then display on UI thread

后端 未结 6 1525
情话喂你
情话喂你 2020-12-10 04:10

UPDATE: Just to summarize what my question has boiled down to:

I was hoping that constructing .NET forms and controls did NOT create any window handles -- hoping tha

6条回答
  •  不思量自难忘°
    2020-12-10 04:45

    I believe it is possible to add the components created on the non-UI thread to the main UI, I've done it.

    So there are 2 threads, 'NewCompThread' and 'MainThread'.

    You spin off NewCompThread and it creates components for you - all ready to be displayed on the MainUI (created on MainThread).

    But ... you WILL get an exception if you try something like this on NewCompThread: ComponentCreatedOnNewCompTHread.parent = ComponentCreatedOnMainThread;

    But you can add this:

    if (ComponentCreatedOnMainThread.InvokeRequired) {
      ComponentCreatedOnMainThread.Invoke(appropriate delegate...);
    } else {
      ComponentCreatedOnNewCompTHread.parent = ComponentCreatedOnMainThread; 
    }
    

    And it will work. I've done it.
    The strange thing (to me) is that then the ComponentCreatedOnNewCompTHread 'thinks' it was created on the MainThread.

    If you do the following from the NewCompThread: ComponentCreatedOnNewCompTHread.InvokeRequired it will return TRUE, and you'll need to create a delegate and use Invoke to get back to the MainThread.

提交回复
热议问题