Microsoft KB Archive/315460

= How to submit a work item to the thread pool by using Visual C# =

Article ID: 315460

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q315460



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Threading

IN THIS TASK
SUMMARY
 * Requirements
 * Create a Visual C# Application that Uses the Thread Pool
 * Test the Sample



SUMMARY
This step-by-step article shows you how to submit a method to the thread pool for execution.

In the .NET environment, each process has a thread pool that you can use to run methods asynchronously.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * The Visual C# programming language

back to the top

Create a Visual C# Application that Uses the Thread Pool
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Visual C# Windows Application project named PoolDemo. Use the Toolbox to add a Button control to the form. The default name for the Button control is button1. Right-click the form, and then click View Code.  Paste the following using directive after the existing using directives, but before the declaration of the PoolDemo namespace: using System.Threading;   Switch back to Design view, and then double-click button1. Paste the following code in the button1_Click event handler: private void button1_Click(object sender, System.EventArgs e) { WaitCallback wcb = new WaitCallback(GetSysDirSize); try {       ThreadPool.QueueUserWorkItem(wcb); MessageBox.Show(&quot;The work item has been placed on the queue&quot;); }   catch (Exception ex) {       MessageBox.Show(&quot;Error: &quot; + ex.Message); } }                     Paste the following code within the body of the Form1 class. The GetSysDirSize method calculates the total number of bytes that are stored in the system directory. GetSysDirSize calls another method named DirSize to perform the calculation.

NOTE: This task might take some time to run. private void GetSysDirSize(object state) {   long total_length = DirSize(Environment.SystemDirectory); this.Text = total_length.ToString; }

private long DirSize(string path) {   long sz = 0; System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(path); // List files. foreach(System.IO.FileInfo f in d.GetFiles) {       sz += f.Length; }

// Recurse into directories. foreach(System.IO.DirectoryInfo dx in d.GetDirectories) {       sz += DirSize(dx.FullName); }

return sz; }                   </li></ol>

back to the top

Test the Sample

 * 1) Press CTRL+F5 to run the application.
 * 2) When the form appears, click the button. When the The work item has been placed on the queue message box appears, click OK to dismiss the message box and return to the main form. After a short delay, the total file size in the system directory is displayed in the caption of the form. The length of the delay depends on the speed of your computer and the number of files in the system directory. The calculation of file sizes takes place on a thread in the thread pool.

back to the top

Keywords: kbhowtomaster kbsample kbthread KB315460

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.