Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:13:05+00:00 2026-05-16T07:13:05+00:00

I have invoked BeginInvoke on 10 delegates in a loop. Instead of using 10

  • 0

I have invoked BeginInvoke on 10 delegates in a loop. Instead of using 10 threads, the threadpool uses only two/three threads for executing the delegates. Can somebody please explain the reason for this?. The delegate execution takes only a few ms (less than 10ms).

When I logged threadpool parameters before invoking BeginInvoke it indicated that Min Threads = 2, Max Threads = 500, Available threads = 498.


I got the problem when I invoked the following managed c++ code.

void EventHelper::FireAndForget(Delegate^ d, ... array<Object^>^ args)
            {
                try
                {
                    if (d != nullptr)
                    {                                           
                        array<Delegate^>^ delegates = d->GetInvocationList();   

                        String^ message1 = String::Format("No of items in the event {0}",delegates.Length);
                        Log(LogMessageType::Information,"EventHelper.FireAndForget", message1);

                        // Iterating through the list of delegate methods.
                        for each(Delegate^ delegateMethod in delegates)
                        {
                            try
                            {
                                int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                                ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                                ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                                ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                                String^ message = String::Format("FireAndForget Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);

                                DynamicInvokeAsyncProc^ evtDelegate = gcnew DynamicInvokeAsyncProc(this, &EventHelper::OnTriggerEvent);
                                evtDelegate->BeginInvoke(delegateMethod, args, _dynamicAsyncResult, nullptr); //FIX_DEC_09 Handle Leak    
                            }
                            catch (Exception^ ex)
                            {
                                String^ message = String::Format("{0} : DynamicInvokeAsync of '{1}.{2}' failed", _id,
                                                                    delegateMethod->Method->DeclaringType, d->Method->Name);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);                              
                            }
                        }
                    }
                    else
                    {                   
                    }
                }
                catch (Exception^ e)
                {
                    Log(LogMessageType::Error, "EventHelper.FireAndForget", e->ToString());
                }

            }

This is the method given in the delegate

void EventHelper::OnTriggerEvent(Delegate^ delegateMethod, array<Object^>^ args)
            {
                try
                {
                    int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                    ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                    ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                    ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                    String^ message = String::Format("OnTriggerEvent Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                    delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    message = String::Format("Before Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    // Dynamically invokes (late-bound) the method represented by the current delegate. 
                    delegateMethod->DynamicInvoke(args);
                    message = String::Format("After Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);
                }
                catch (Exception^ ex)
                {
                    Log(LogMessageType::Error, "EventHelper.OnTriggerEvent", ex->ToString());
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:13:06+00:00Added an answer on May 16, 2026 at 7:13 am

    You wouldn’t want 10 threads to be created for this. The optimal situation is to have as many active threads as you have cores. You’ll find that ThreadPool.MinThreads equals the # of logical CPU’s on your PC.

    Additional Threads will be created but the ThreadPool delays that on purpose. The algorithm in Fx4 has been improved, see this page. A quick look at the picture at the bottom will help you understand the principle.

    Extra threads are only helpful to compensate for blocked threads, but this is difficult to get exactly right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.