IOCP Thread Pooling in C# - Part II - The Sample Application
(Page 2 of 3 )
Start by adding a new class to your C# project. Remove all of the code provided by the Visual Studio .NET wizard. Then add all of the following code. In Main, an IOCP thread pool is created, and a single piece of work is posted to the IOCP thread pool. We pass the data value of 10 along with the posted work.
The main thread is then put to sleep. This gives the IOCP thread function time to wake up to process the work posted. The last thing in main is to dispose the IOCP thread pool. The IOCP thread function displays the value of the data passed into the IOCP thread pool.
using System;
using System.Threading; // Included for the Thread.Sleep call
using Continuum.Threading;
namespace Sample
{
//============================================
/// <summary> Sample class for the threading class </summary>
public class UtilThreadingSample
{
//*******************************************
/// <summary> Test Method </summary>
static void Main()
{
// Create the MSSQL IOCP Thread Pool
IOCPThreadPool pThreadPool = new IOCPThreadPool(0, 5, 10, new IOCPThreadPool.USER_FUNCTION(IOCPThreadFunction));
pThreadPool.PostEvent(10);
Thread.Sleep(100);
pThreadPool.Dispose();
}
//*****************************************
/// <summary> Function to be called by the IOCP thread pool. Called when
/// a command is posted for processing by the SocketManager </summary>
/// <param name="iValue"> The value provided by the thread posting the event </param>
static public void IOCPThreadFunction(Int32 iValue)
{
try
{
Console.WriteLine("Value: {0}", iValue);
}
catch (Exception pException)
{
Console.WriteLine(pException.Message);
}
}
}
}
This is what you should see when you run the sample application. On your own change the main function to call the PostEvent method several times and see how the IOCP thread pool performs.
Next: Review >>
More C# Articles
More By William Kennedy