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 7710381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:06:42+00:00 2026-06-01T01:06:42+00:00

I am using System.Threading.ThreadPool.QueueUserWorkItem(x => MyMethod(param1, param2, param3, param4, param5)); I want to call

  • 0

I am using

System.Threading.ThreadPool.QueueUserWorkItem(x => MyMethod(param1, param2, param3, param4, param5));

I want to call the following method from the main thread every time the call to MyMethod is completed:

UpdateGui()
{

}

How do I do that?

Thanks!

  • 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-06-01T01:06:43+00:00Added an answer on June 1, 2026 at 1:06 am

    Keep a global counter of work items queued and an object to protect it:

    int runningTasks = 0;
    object locker = new object();
    

    Every time a task is added increment the counter:

    lock(locker) runningTasks++;
    System.Threading.ThreadPool.QueueUserWorkItem(x => MyMethod(param1, param2, param3, param4, param5));
    

    At the end of MyMethod decrement the counter and signal the main thread:

    lock(locker) 
    {
        runningTasks--;
        Monitor.Pulse(locker);
    }
    

    In the main thread (assuming this is not the GUI thread!):

    lock(locker)
    {
        while(runningTasks > 0)
        {
            Monitor.Wait(locker);            
            UpdateGUI();
        }
    }
    

    This way you also have a barrier to wait for all pending tasks to finish.

    In case you don’t want to wait, just skip the main thread completely and call UpdateGUI to forward updates to the GUI thread when MyMethod finishes.

    Note that inside MyMethod you should have some form of Dispatcher.BeginInvoke (WPF) or Control.BeginInvoke (WinForms) otherwise you cannot update the GUI safely!

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

Sidebar

Related Questions

i got a code to invoke any method through thread. System.Threading.ThreadPool.QueueUserWorkItem(Export()); here i tried
I am using a System.Threading.ThreadPool to manage a queue of jobs from a service.
Using a System.Threading.Timer results in threads being spun from a ThreadPool , which means
using System; using System.Threading; // Simple threading scenario: Start a static method running //
The following code using System.Threading; class Test { volatile int counter = 0; public
I am using System.Threading.Timer in my Windows service and locking the callback method using
When using System.Threading.Timer, and initializing a windows form on the timer's tick event, the
using System; using System.Drawing; using System.IO; using System.Security.Cryptography; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Threading;
The Code using System; using System.Threading; public delegate void LoadingProgressCallback(double PercentComplete,string ItemName); public delegate
I have an issue with System.Threading.Timer. I am scheduling some actions using a time

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.