A little experiment to verify most asynchronous operations do execute on some ThreadPool thread. category 'experiment', language C#, created 22-Jun-2009, version V1.0, by Luc Pattyn |
License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author. Disclaimer: This work is provided |
The .NET Framework offers lots of classes that perform some operations asynchronously, i.e. they organize something to happen and don't wait for the outcome.
When asynchronous operations need to execute some user code, they need a thread to execute it; it typically can't be the main thread since that is either busy doing other things, and/or is kept free for servicing the GUI. What the MSDN documentation does not tell us clearly is such operations typically get executed on ThreadPool threads.
The AsyncOnThreadPool experiment provides a simple means to observe how those asynchronous operations are carried out.
The test application basically has a button handler that causes several asynchronous actions to be launched; all activity gets logged in the ListBox while showing the actual time (a StopWatch value since app start) and the thread ID of the executing thread, coded as follows:
[GUI]
, when it is the main aka GUI thread;[Pxx]
, when it is a ThreadPool thread with ID=xx;[ xx]
, when it is a general thread with ID=xx.An optional Thread.Sleep(500) is added to the asynchronous code, allowing to modify the availability of some of the threads when the next asynchronous action might occur.
The project files are available here.
System.Windows.Forms.Timer.Tick
System.ComponentModel.BackgroundWorker.ProgressChanged
System.ComponentModel.BackgroundWorker.RunWorkerCompleted
DrawMode.OwnerDrawFixed
has been selected, so we could
have the DrawItem
event paint text lines using different colors.StopWatch
has been used instead of reading DateTime.Now
in order to get an actual timing reolution of
one millisecond.Perceler |
Copyright © 2012, Luc Pattyn |
Last Modified 20-Dec-2024 |