Opening a form in a new thread, appart from the Apps main thread 
Author Message
 Opening a form in a new thread, appart from the Apps main thread

Hi,

Is it possible to have a form running in a new thread ?
Instead of running just some subs in a new thread.

So that the 2 forms both run in their own thread and don't take eachothers
runtime ?

Thnx.



Fri, 22 Apr 2005 21:42:22 GMT  
 Opening a form in a new thread, appart from the Apps main thread
Yes. Start a new thread in the usual way and then create and show a form.
You must show the form using ShowDialog so that a message pump is created
for the form, otherwise the thread will just exit and your form will be
gone. The ShowDialog method is modal only on the thread that created it so
your original form on your main thread will still accept input.

All that being said this is a bad idea - for anything beyond the trivial
you're likely to end up with an unmaintainable mess. Use threads to manage
TASKS independently(if it's really necessary, it frequently isn't) not
FORMS.


Quote:
> Hi,

> Is it possible to have a form running in a new thread ?
> Instead of running just some subs in a new thread.

> So that the 2 forms both run in their own thread and don't take eachothers
> runtime ?

> Thnx.



Fri, 22 Apr 2005 23:00:25 GMT  
 Opening a form in a new thread, appart from the Apps main thread
Hi,

Could you give me some more advice.

You see the application I'm creating is infact a container application for
multiple small applications (within the same exe)

One of them is an IRC client, this is the one I want to run in a different
thread, because this form is contantly updated
with the chat, in the meantime I want to be able to use the other functions
in the application without any inteference from the
Chat.

Thnx.



Quote:
> Yes. Start a new thread in the usual way and then create and show a form.
> You must show the form using ShowDialog so that a message pump is created
> for the form, otherwise the thread will just exit and your form will be
> gone. The ShowDialog method is modal only on the thread that created it so
> your original form on your main thread will still accept input.

> All that being said this is a bad idea - for anything beyond the trivial
> you're likely to end up with an unmaintainable mess. Use threads to manage
> TASKS independently(if it's really necessary, it frequently isn't) not
> FORMS.



> > Hi,

> > Is it possible to have a form running in a new thread ?
> > Instead of running just some subs in a new thread.

> > So that the 2 forms both run in their own thread and don't take
eachothers
> > runtime ?

> > Thnx.



Fri, 22 Apr 2005 23:19:16 GMT  
 Opening a form in a new thread, appart from the Apps main thread
I forgot to mention something important

I'm working with an MDI form, which should stay the MDI parent for the form
created in the new thread
Still possible???



Fri, 22 Apr 2005 23:24:51 GMT  
 Opening a form in a new thread, appart from the Apps main thread
You can't create MDIChild windows on a thread different from that of their
parent.

As far as your Chat applet goes you need to separate the GUI updates from
the IRC processing task.  The UI updating shouldn't interfere with the
operation of the rest of your GUI unless you are talking about hundreds or
more of updates per second(and even then you can buffer the updates) so
there is no need to put the form itself into another thread. On the other
hand the IRC processing may need to involve another thread(s) if its load
will be very heavy, if the task involves many waits(for disk/network
IO,etc), if it will use blocking sockets calls, etc. If this is the case use
another thread(or thread pool threads) and asynchronous sockets calls within
your IRC client object(s) and then use Invoke (or better yet
Begin/EndInvoke) to marshal the data back to the main thread in order to
update the UI.


Quote:
> Hi,

> Could you give me some more advice.

> You see the application I'm creating is infact a container application for
> multiple small applications (within the same exe)

> One of them is an IRC client, this is the one I want to run in a different
> thread, because this form is contantly updated
> with the chat, in the meantime I want to be able to use the other
functions
> in the application without any inteference from the
> Chat.

> Thnx.



> > Yes. Start a new thread in the usual way and then create and show a
form.
> > You must show the form using ShowDialog so that a message pump is
created
> > for the form, otherwise the thread will just exit and your form will be
> > gone. The ShowDialog method is modal only on the thread that created it
so
> > your original form on your main thread will still accept input.

> > All that being said this is a bad idea - for anything beyond the trivial
> > you're likely to end up with an unmaintainable mess. Use threads to
manage
> > TASKS independently(if it's really necessary, it frequently isn't) not
> > FORMS.



> > > Hi,

> > > Is it possible to have a form running in a new thread ?
> > > Instead of running just some subs in a new thread.

> > > So that the 2 forms both run in their own thread and don't take
> eachothers
> > > runtime ?

> > > Thnx.



Sat, 23 Apr 2005 01:13:27 GMT  
 Opening a form in a new thread, appart from the Apps main thread
Is There any purpose on doing this?
If you show the form as a dialog then the program stops until the forms
unload
Im not clear about the benefit of using a new thread there.

any comments?

kind regards to all
Francisco


Quote:
> Yes. Start a new thread in the usual way and then create and show a form.
> You must show the form using ShowDialog so that a message pump is created
> for the form, otherwise the thread will just exit and your form will be
> gone. The ShowDialog method is modal only on the thread that created it so
> your original form on your main thread will still accept input.

> All that being said this is a bad idea - for anything beyond the trivial
> you're likely to end up with an unmaintainable mess. Use threads to manage
> TASKS independently(if it's really necessary, it frequently isn't) not
> FORMS.



> > Hi,

> > Is it possible to have a form running in a new thread ?
> > Instead of running just some subs in a new thread.

> > So that the 2 forms both run in their own thread and don't take
eachothers
> > runtime ?

> > Thnx.



Sat, 23 Apr 2005 02:36:58 GMT  
 Opening a form in a new thread, appart from the Apps main thread
I wasn't planning on showing the form as a dialog



Quote:
> Is There any purpose on doing this?
> If you show the form as a dialog then the program stops until the forms
> unload
> Im not clear about the benefit of using a new thread there.

> any comments?

> kind regards to all
> Francisco



> > Yes. Start a new thread in the usual way and then create and show a
form.
> > You must show the form using ShowDialog so that a message pump is
created
> > for the form, otherwise the thread will just exit and your form will be
> > gone. The ShowDialog method is modal only on the thread that created it
so
> > your original form on your main thread will still accept input.

> > All that being said this is a bad idea - for anything beyond the trivial
> > you're likely to end up with an unmaintainable mess. Use threads to
manage
> > TASKS independently(if it's really necessary, it frequently isn't) not
> > FORMS.



> > > Hi,

> > > Is it possible to have a form running in a new thread ?
> > > Instead of running just some subs in a new thread.

> > > So that the 2 forms both run in their own thread and don't take
> eachothers
> > > runtime ?

> > > Thnx.



Sat, 23 Apr 2005 03:07:37 GMT  
 Opening a form in a new thread, appart from the Apps main thread
See Inline


Quote:
> Is There any purpose on doing this?

If you want to process the messages from one window while another window is
processing a very long sequence of actions then it would be useful. For
instance if in Form1 you have a button that when clicked calls a procedure
that takes 10 seconds to return but you still want the user to be able to
use Form2 in the meantime then you could use this technique. But as I said
in my original post it would, almost certainly,  be a bad design.

Quote:
> If you show the form as a dialog then the program stops until the forms
> unload

When you show the form as a dialog the thread(not the program) goes into a
modal message loop (this is approximately what happens when you call
Application.Run in your sub Main). So the next statements after that will
not be executed until the thread exits the message loop when the form is
hidden or unloaded. But the form will respond to user actions since these
result in messages being sent to be processed in the message loop. In this
case since the original form is on a different thread it isn't constrained
by the modal message loop and it will continue to responed to user input as
well. If you did not use ShowDialog the same thing would happen as happens
if you do not use Application.Run(FormInstance) in your Sub Main the thread
simply exits and your form is gone.

Quote:
> Im not clear about the benefit of using a new thread there.

I don't actually think there are any REAL benefits here - you'd be better
off designing your application properly.
Quote:

> any comments?

> kind regards to all
> Francisco

> <snip>



Sat, 23 Apr 2005 05:35:58 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. New form on Main thread.

2. Will child-thread end when main thread end?

3. CreateObject, Thread per Object, and the Main Thread

4. NEW THREAD: Promoting Your VB APP ??

5. database, Main Process, Threads

6. Main Window for a thread

7. Safe threading by delivering EVENTS to UI THREAD

8. Threading problem --> threads die

9. Thread error without threads

10. threading - Multiple threads returning values at same time

11. thread spawning a thread?

12. single thread and apartment threads

 

 
Powered by phpBB® Forum Software