473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Do-Nothing WinForm App Using 4 Threads?

I've been researching multi-threaded WinForms apps and thread
synchronization stuff for a couple days since I'm working on
refactoring a multi-threaded GUI app at work and want to be sure it's
rock solid/thread-safe. I've seen all the posts about using BeginInvoke
to have worker threads interact with the UI. My question is this:

I created a plain old Windows Form application (VS.NET 2005) with a
blank form, built it (release build), ran it from Windows Explorer and
Task Manager claims the application is using four threads. What are
they? Obviously one is the main app/UI. What are the others?

I then added a Windows Timer control to the form and updated a label
every second. Still four threads. I expected a timer to be on its own
thread even though it posts "tick" events to the UI message
loop/thread.

I removed the Windows Timer and replaced it with a System Timer which
runs "tick" events on a different thread, yet the thread count still
stays at four. Huh?

Is there a built-in thread pool with a few workers hanging around to do
timers and stuff? What exactly is going on here?
Are those extra threads part of the CLR doing its magic to run the EXE?
Other?

My next question will be how many threads remoting (inter-process on
the same PC) adds to the mix. The UI app I'm refactoring makes use of
several remote objects (running methods and receiving events with
data). I'm guessing there is a thread for each proxy/socket to handle
marshalling stuff. And the event handling from remote objects
supposedly grabs a thread from the thread pool. Inquiring minds what to
really KNOW what's going on under the hood.

Pointers to good articles and/or books welcome. Spend the last weekend
Googling and reading...

Thanks!

Jan 15 '07 #1
4 3964
gsimmons wrote:
I've been researching multi-threaded WinForms apps and thread
synchronization stuff for a couple days since I'm working on
refactoring a multi-threaded GUI app at work and want to be sure it's
rock solid/thread-safe. I've seen all the posts about using BeginInvoke
to have worker threads interact with the UI. My question is this:

I created a plain old Windows Form application (VS.NET 2005) with a
blank form, built it (release build), ran it from Windows Explorer and
Task Manager claims the application is using four threads. What are
they? Obviously one is the main app/UI. What are the others?
Someone will be along shortly with a more in depth answer, but broadly
speaking: The objects of type System.Threading.Thread that application
developers are concerned with do *not* map one-to-one with the Win32
entities known as 'threads'. The latter is what Task Manager is telling
you about; the former are the things for which we have to have concern
for thread safety.

For background reading on this I would start with Richter ('CLR via
C#'), though there may well be even more advanced books specifically
about concurrent programming in .NET.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 15 '07 #2
Hi,

The .NET threads are not phisically related to the one from win. For example
there is nothing like ThreadPool in native win.

At least one of those thread that you are seeing must be running the GC.

Take a look at this link ; it refers to the CF but I bet something similar
should apply to the full framework

Threads
There are up to four threads created by a .NET Compact Framework
application:

a.. A main application thread.

b.. A thread used to control various period timers and time-outs that can
be scheduled by the system or applications.

c.. A thread used to track changes to the active TCP/IP interfaces
(simulating the media sense behavior that is present on Windows XP but not
Windows CE).

d.. A thread that is used to run object finalizers. It is created when the
first finalizable object is garbage collected.

For more information about threading support, see Threading in the .NET
Compact Framework.

"gsimmons" <si***********@gmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
| I've been researching multi-threaded WinForms apps and thread
| synchronization stuff for a couple days since I'm working on
| refactoring a multi-threaded GUI app at work and want to be sure it's
| rock solid/thread-safe. I've seen all the posts about using BeginInvoke
| to have worker threads interact with the UI. My question is this:
|
| I created a plain old Windows Form application (VS.NET 2005) with a
| blank form, built it (release build), ran it from Windows Explorer and
| Task Manager claims the application is using four threads. What are
| they? Obviously one is the main app/UI. What are the others?
|
| I then added a Windows Timer control to the form and updated a label
| every second. Still four threads. I expected a timer to be on its own
| thread even though it posts "tick" events to the UI message
| loop/thread.
|
| I removed the Windows Timer and replaced it with a System Timer which
| runs "tick" events on a different thread, yet the thread count still
| stays at four. Huh?
|
| Is there a built-in thread pool with a few workers hanging around to do
| timers and stuff? What exactly is going on here?
| Are those extra threads part of the CLR doing its magic to run the EXE?
| Other?
|
| My next question will be how many threads remoting (inter-process on
| the same PC) adds to the mix. The UI app I'm refactoring makes use of
| several remote objects (running methods and receiving events with
| data). I'm guessing there is a thread for each proxy/socket to handle
| marshalling stuff. And the event handling from remote objects
| supposedly grabs a thread from the thread pool. Inquiring minds what to
| really KNOW what's going on under the hood.
|
| Pointers to good articles and/or books welcome. Spend the last weekend
| Googling and reading...
|
| Thanks!
|
Jan 15 '07 #3
"gsimmons" <si***********@gmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
I've been researching multi-threaded WinForms apps and thread
synchronization stuff for a couple days since I'm working on
refactoring a multi-threaded GUI app at work and want to be sure it's
rock solid/thread-safe. I've seen all the posts about using BeginInvoke
to have worker threads interact with the UI. My question is this:

I created a plain old Windows Form application (VS.NET 2005) with a
blank form, built it (release build), ran it from Windows Explorer and
Task Manager claims the application is using four threads. What are
they? Obviously one is the main app/UI. What are the others?

I then added a Windows Timer control to the form and updated a label
every second. Still four threads. I expected a timer to be on its own
thread even though it posts "tick" events to the UI message
loop/thread.

I removed the Windows Timer and replaced it with a System Timer which
runs "tick" events on a different thread, yet the thread count still
stays at four. Huh?

Is there a built-in thread pool with a few workers hanging around to do
timers and stuff? What exactly is going on here?
Are those extra threads part of the CLR doing its magic to run the EXE?
Other?

My next question will be how many threads remoting (inter-process on
the same PC) adds to the mix. The UI app I'm refactoring makes use of
several remote objects (running methods and receiving events with
data). I'm guessing there is a thread for each proxy/socket to handle
marshalling stuff. And the event handling from remote objects
supposedly grabs a thread from the thread pool. Inquiring minds what to
really KNOW what's going on under the hood.

Pointers to good articles and/or books welcome. Spend the last weekend
Googling and reading...

Thanks!

A managed application always starts with a minimum of 3 threads:
1 is the main thread
2 is the debugger thread
3 is the finalizer thread.

A Windows Forms application has at least one additional thread;
4 the GDI+ rendering thread.
Threads 1 and 3 are OS threads, associated with a logical (CLR) thread , while 2 and 4 are
just native OS threads created by the CLR, they don't have an associated logical thread.

Non "Windows Forms Timers" are handled by the Threadpool, more exactly their handlers run on
an IO completion thread pulled from the pool. It's obvious that you will only notice these
threads when they run for at least Taskman's window update interval .
Windows Forms Timer handlers are run on the UI thread, they are simple WM_TIMER handlers.

Willy.

Jan 15 '07 #4
Thanks for the replies folks. The help is much appreciated. I was
getting a little concerned when I see our application using 13 or 14
threads in Task Manager and I was (still am) trying to learn where
they're all coming from. I'll add some remoting to my stupid demo app
and see if I can figure out where in all that auto-magic proxy stuff
the threads are being used. I guess all I really need to know/care
about is:

- the UI runs on the main thread
- Windows.Form timer event handlers are on the UI thread
- System timers are on a thread pool thread
- Remote event handlers are on a thread pool thread
- BeginInvoke is your friend

but I like to understand what's going on under the hood too. Time to
dig out a box of books from the last job. I think Richters .NET
programming book is in there somewhere...

Garry

Jan 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
2
by: Sune | last post by:
Hi, I need to do a loop with some criterias that depends on an earlier database query. I tried to do it this way, but it seems not to accept that the do is inside the if: if not objrs.EOF...
96
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
4
by: Dave | last post by:
If so, what is wrong with this code in APS 3.0? I open a recordset with order info. Each record contains the order header info plus a line item from the order detail. So the recordset looks...
5
by: é›· | last post by:
suggest add do while loop in later version
6
by: Kc-Mass | last post by:
I have a flat file Access database that I am trying to normalize. It has one collection of fields named "Project 1 Sponsor & Date" thru "Project 14 Sponsor & Date". These are essentially...
0
by: WORKING IN FAITH | last post by:
" Interviewing Seminar LEADERS; How did I do that ? Faith that's HOW... " Inbox Craig Somerford to Apple, Katrinas, Bruno, Wiz, Kettering, City, Katie, CBC, Harvard, (bcc:Local), (bcc:Local),...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.