loading form from another thread

R

Roman

Hello

i have main mdi parent window from which i want to load a form, but
during creating form object and later calling its Show method i want
to display progress and have the main window responsive. But the problem is
that i cannot load form from another thread, when i try to set its
MdiParent property to main window, i get message that parent and child
cant be on separate threads. I could invoke Show without setting MdiParent,
but when i do that the window dont show up cause the thread terminates
after calling Show, and if i try to suspend it, i get only a part of my
form,
which is not responding. I know that this child form must have a message
loop. For now i create and set whole needed properties of window in separete
thread, and just calling Show in main thread, and it works.
But the best for me would be something like that: i create and invoke Show
on separete
thread, and later eventually i link somehow my child window with parent and
its thread.
thanks for any advices
best regards
roman
 
J

Jon Skeet [C# MVP]

Roman said:
i have main mdi parent window from which i want to load a form, but
during creating form object and later calling its Show method i want
to display progress and have the main window responsive. But the problem is
that i cannot load form from another thread, when i try to set its
MdiParent property to main window, i get message that parent and child
cant be on separate threads. I could invoke Show without setting MdiParent,
but when i do that the window dont show up cause the thread terminates
after calling Show, and if i try to suspend it, i get only a part of my
form, which is not responding.

<snip>

I don't believe you can do the UI part of what you want on a different
thread. However, what exactly is taking a long time when you're
creating the form? Is it doing stuff like fetching from a database? If
so, you could put *that* part into another thread, then create the form
itself on the main UI thread when the rest of the work has been done.

Jon
 
R

Roman

I don't believe you can do the UI part of what you want on a different
thread. However, what exactly is taking a long time when you're
creating the form? Is it doing stuff like fetching from a database? If
so, you could put *that* part into another thread, then create the form
itself on the main UI thread when the rest of the work has been done.

the problem is that i want to load not the forms i know, but the foreign
forms that i cant make any assumptions about. i think the most workload
there will be placed in load event of the form. i thought about calling load
handlers myself from loading separate thread but, they might also need
some controls ready and loaded, which means i probably should call it only
after form
is displayed by show. i was just wonder if it is possible to fully create
form
in other thread and then link it somehow to main ui thread
regards
roman
 
J

Jon Skeet [C# MVP]

Roman said:
the problem is that i want to load not the forms i know, but the foreign
forms that i cant make any assumptions about. i think the most workload
there will be placed in load event of the form. i thought about calling load
handlers myself from loading separate thread but, they might also need
some controls ready and loaded, which means i probably should call it only
after form is displayed by show. i was just wonder if it is possible to fully
create form in other thread and then link it somehow to main ui thread

I don't believe it is - unless there's some devious call in the Win32
API to "reparent" a UI in a different thread.
 
R

Roman

I don't believe it is - unless there's some devious call in the Win32
API to "reparent" a UI in a different thread.

ok, thanks for you answer : )
lukasz cygan (roman)
 
M

Marc Gravell

As an implementation idea - you could use events - i.e. when the call finds
it needs to create a form it simply fires the event (indicating "what" on
the event args); to existing UI can catch this, Invoke to itself (to switch
threads) and then create the form. This keeps UI in the UI and data in the
data-tier.

Perhaps ;-0

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top