threading in winforms and the timer control

M

Mattias Sjögren

1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2
No


2. if you have a timer control running on a childform, and you either
a. minimize or

b. hide the form or

c. the form is no longer the currently selected form ,


does the timer keep running or does it stop ?

It keeps running.



3. if you have a timer or a thread running on a child form, and you
close the form or end the application, do you have to stop the thread
or the timer beforehand or does .net cleanup and sort things for you ?

You may have to stop the thread (if it's not marked as a background
thread). You probably don't have to stop the timer, but it certainly
doesn't hurt doing so.


Mattias
 
P

Peted

Just some threading questions


1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2

2. if you have a timer control running on a childform, and you either
a. minimize or

b. hide the form or

c. the form is no longer the currently selected form ,


does the timer keep running or does it stop ?

3. if you have a timer or a thread running on a child form, and you
close the form or end the application, do you have to stop the thread
or the timer beforehand or does .net cleanup and sort things for you ?



thanks


Peted
 
J

Jon Skeet [C# MVP]

Just some threading questions

1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2

No, unless you've specifically put them in different threads, they'll
be in their own thread.
2. if you have a timer control running on a childform, and you either
a. minimize or

b. hide the form or

c. the form is no longer the currently selected form ,

does the timer keep running or does it stop ?

I believe it keeps running (it would be odd not to) - but that's easy
for you to test.
3. if you have a timer or a thread running on a child form, and you
close the form or end the application, do you have to stop the thread
or the timer beforehand or does .net cleanup and sort things for you ?

The timer will be disposed automatically, and even if somehow it isn't,
it isn't running in a separate foreground thread anyway, so you don't
need to worry.
 
A

Alberto Poblacion

1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2

They al use the same thread. No problem updating one child form from
another.
2. if you have a timer control running on a childform, and you either
a. minimize or
b. hide the form or
c. the form is no longer the currently selected form ,
does the timer keep running or does it stop ?

It keeps running.
3. if you have a timer or a thread running on a child form, and you
close the form or end the application, do you have to stop the thread
or the timer beforehand or does .net cleanup and sort things for you ?

It depends on how you start the Thread. If you set the IsBackground
property of the thread object to true, then the thread is automatically
stoped when you stop the main program. Otherwise, you have to be careful,
because the thread will continue to run in the background even if you close
all visible forms. No such problem with the Timer Control, which is Disposed
when the form that contains it is Disposed.
 
J

Jon Skeet [C# MVP]

<Peted> wrote:

3. if you have a timer or a thread running on a child form, and you
close the form or end the application, do you have to stop the thread
or the timer beforehand or does .net cleanup and sort things for you ?

Sorry, I only addressed this from the point of view of a WinForms
timer. If you've created a separate thread, and haven't set
IsBackground to true, you'll need to stop the thread.

See http://pobox.com/~skeet/csharp/threads/shutdown.shtml for a pattern
of terminating threads cleanly.
 

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