using System;using System.Threading;namespace NewThreadPoolBehavior{ internal class Program { private static void Main(string[] args) { Console.WriteLine( "Running on " + Environment.Version ); int w, c; ThreadPool.GetMaxThreads( out w, out c ); Console.WriteLine( "Max Currently: " + w + ", " + c ); ThreadPool.GetMinThreads( out w, out c ); Console.WriteLine( "Min Currently: " + w + ", " + c ); Console.WriteLine( "Set min thread count 20? (y/n) " ); string txt = Console.ReadLine(); if ( txt == "y" ) { Console.WriteLine( "Setting min to 20" ); ThreadPool.SetMinThreads( 20, 100 ); ThreadPool.GetMinThreads( out w, out c ); Console.WriteLine( "Min Currently: " + w + ", " + c ); } UseThreadPool( 200 ); Console.ReadLine(); } private static DateTime startTime; private static void UseThreadPool(int count) { startTime = DateTime.Now; for ( int i = 0; i < count; i++ ) { ThreadPool.QueueUserWorkItem( delegate { SlowMethod(); } ); } } private static int concurrent = 0; private static void SlowMethod() { TimeSpan dt = DateTime.Now - startTime; concurrent++; Console.WriteLine( "Starting ops (" + concurrent + " concurrent, elapsed=" + dt.TotalSeconds.ToString( "N3" ) + " sec.) " ); Thread.Sleep( 20000 ); Console.WriteLine( "Finished ops (" + concurrent + " concurrent)" ); concurrent--; } }}
Posted in DevelopMentor | Visual Studio |Comments [2]
A blog by Michael Kennedy
Speaking Events
About: Michael is an instructor for DevelopMentor, a .NET enthusiast, an agile pioneer, an entrepreneur, a father of three girls, a husband, a student, and a teacher.
Explore Related Content
Concepts >> azure - Workflow - RESTful - Unit Testing - web - WCF - ASP.Net - MVC - LINQ
Tools >> Visual Studio ClickOnce IIS
Type >> Screencast - Tools - Article - How To - Course
@dm_the_company @mkennedy @andrewclymer @bmaso @danamiga @georgeshep @isidore_us @jason_diamond @markblomsma @marksm @mauricedb @pierrenallet @richardblewett @tonysneed @wallacekelly
Sign In