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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:11+00:00 2026-05-25T21:45:11+00:00

When i use invoke inside AddListBoxItem function as seen below software become unreponsive and

  • 0

When i use invoke inside AddListBoxItem function as seen below software become unreponsive and frozen but if i use BeginInvoke it works. Why is that happening ?

visual studio 2010 , C# 4.0

 private void button2_Click(object sender, EventArgs e)
{
    var watch = Stopwatch.StartNew();
    Parallel.For(2, 20, (i) =>
    {
        var result = SumRootN(i);
        AddListBoxItem("root " + i + " : " + result);
    });
    AddListBoxItem(watch.ElapsedMilliseconds.ToString());            
}

private delegate void AddListBoxItemDelegate(object item);

private void AddListBoxItem(object item)
{
    if (this.listBox1.InvokeRequired)
    {
        this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item);
    }
    else
    {
        this.listBox1.Items.Add(item);
    }
}
  • 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-25T21:45:12+00:00Added an answer on May 25, 2026 at 9:45 pm

    Your UI thread will wait for Parallel.For to complete before it continues. That means it can’t process any further UI messages until it’s completed.

    Now when the worker threads call Invoke, they wait until the UI thread processes the delegate before they continue. So they’re waiting for the UI thread to get free, basically.

    Hence, you have a deadlock – the UI thread is waiting for the tasks, which are waiting for the UI thread… BeginInvoke works because then the task threads don’t wait for the delegates to be processed in the UI thread.

    I would suggest that you don’t call Parallel.For in the UI thread to start with. You’ll block the UI until it completes anyway, which isn’t a good idea. Do the whole thing in a background thread – then you can still use Invoke if you want, and while it’s doing the computation the UI will still be responsive.

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

Sidebar

Related Questions

I have a function that I want to invoke every x seconds, but I
I am trying to use Desktop .browse() to invoke a URL, this works fine
How would I use DLLImport pinvoke to invoke a function i wrote in a
From what I've found in C#, the Control.Invoke method requires that you use a
I have an application that I use to run Exchange Powershell commands inside C#
Is there a .NET version of GetPrivateProfileString? (I can use p/invoke, I just don't
As I understand it I can use reverse P/Invoke to call C# from C++.
I know how to use UIWebView and can invoke a WebView if it is
I have an app that is making use of photos stored on either the
I have a C# program that executes a thread. Inside this thread, I have

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.