473,770 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4016
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.Threadin g.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*******@googl email.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******** *************@s 34g2000cwa.goog legroups.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******** *************@s 34g2000cwa.goog legroups.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
2297
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. This works fine, EXCEPT the mysql statement isn't finished by the time the main loop is called. This is a problem because the main loop actually queries that same table to show the value of the rows you just changed. If I refresh the page...
2
3849
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 then Do until updatearray(0) < objrs("updated") else Do While not fso.AtEndOfStream
96
5710
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 where surgeries.id = animals.id and only where the surgery date was date_a or date_b. I'm doing this in Microsoft Access 2000 and am tearing out my hair
6
71981
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
1955
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 something like this: Orderid PartID 1 12 1 87 2 99
5
12243
by: é›· | last post by:
suggest add do while loop in later version
6
1468
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 freeform fileds. Sometimes the date is in front sometimes in the middle sometims in the end. I have a routine done that walks through the up to 254 characters and picks out the date. What I am trying to do is walk through the variations of the...
0
1266
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), (bcc:Craig) More options 11:59 am (0 minutes ago) 04 nov 2006 Believe it john 16 : 7 http://hlavolamy.szm.sk/brainteasers/optical-illusions.htm
6
2052
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 your business layer and/or data access layer? suppose in my data access layer, I provide try and catch, log the exception and re-throw it back to business layer, then in yoru business layer, what do you do? throw it back to the code behind or...
11
25543
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 semicolon after the (x < 3)? Why can't the
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.