Click here to Skip to main content
15,564,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I test an example from this article.

I wrap the logic into a console program as below, but it does not print out any message.
where is wrong? I can not figure it why:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace ThreadPool01
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            Console.WriteLine("testing thread pool numbers:");
            p.CallFoo30AsyncTimes();
            Console.ReadKey();
        }

        private void CallFoo30AsyncTimes()
        {
            // create a delegate of MethodInvoker
            // poiting to our Foo function.
            MethodInvoker simpleDelegate =  new MethodInvoker(Foo);

            // Calling Foo Async 30 times.
            for (int i = 0; i < 30; i++)
            {
                // call Foo()
                simpleDelegate.BeginInvoke(null, null);
            }
        }

        private void Foo()
        {
            int intAvailableThreads, intAvailableIoAsynThreds;

            // ask the number of avaialbe threads on the pool,
            //we really only care about the first parameter.
            ThreadPool.GetAvailableThreads(out intAvailableThreads, out intAvailableIoAsynThreds);

            // build a message to log
            string strMessage =
                String.Format(@"Is Thread Pool: {0}, Thread Id: {1} Free Threads {2}",
                    Thread.CurrentThread.IsThreadPoolThread.ToString(), Thread.CurrentThread.GetHashCode(), intAvailableThreads);

            // check if the thread is on the thread pool.
            Trace.WriteLine(strMessage);

            // create a delay...
            Thread.Sleep(3000);

            return;
        }
    }
}


What I have tried:

can not figure it out in short time and seek help here.
Posted
Updated 12-Mar-22 22:25pm
Comments
Dave Kreskowiak 13-Mar-22 0:50am    
If you're going to ask questions about an article, do in the forum at the bottom of the article.

Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 
Comments
Southmountain 13-Mar-22 19:18pm    
since that article is old post, I doubt the author would care. so I give a shot here.
thank you for your comments!
The code works (.NET 4.7.2 and Windows 10) but the output is not sent to the console, so just use:
Console.WriteLine(strMessage);

instead of:
Trace.WriteLine(strMessage);
 
Share this answer
 
Comments
Southmountain 13-Mar-22 21:16pm    
thank you! it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900