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

  • SEARCH
  • Home
  • 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 7740083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:41:02+00:00 2026-06-01T08:41:02+00:00

I have developed a windows forms c# application, i just want update items in

  • 0

I have developed a windows forms c# application, i just want update items in a Listbox in the main form by spin offing another thread without blocking the GUI form.
Since threads cannot access form entities like listbox, i thought of using delegates.
Following code in the below shows how i used a delegate to do that task, but it blocks the GUI form. so i just want to convert it to an asynchronous delegate which updates list box without blocking the GUI Form

delegate declaration

 delegate void monitoringServiceDel();

calling the delegate

new monitoringServiceDel(monitoringService).BeginInvoke(null, null);

delegate method implementation

private void monitoringService()
{
        this.listEvents.Invoke(new MethodInvoker(delegate()
        {
            int i = 0 ;
            while (i<50)
            {

                listEvents.Items.Add("count :" + count++);
                Thread.Sleep(1000);
                i ++;
            }

        }));


}
  • 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-01T08:41:03+00:00Added an answer on June 1, 2026 at 8:41 am

    For Win Forms you’ll need to use the Control’s Invoke method:

    Executes the specified delegate on the thread that owns the control’s
    underlying window handle

    The basic scenario is:

    • Do the heavy lifting work with a BackgroundWorker to retrieve all of your items on a non UI blocking thread.
    • On the BackgroundWorker.RunWorkerCompleted Event, use the Control’s Invoke method to add the items to the Control (ListBox in your case).

    Something along the lines of:

    var bw = new BackgroundWorker();
    bw.DoWork += (sender, args) => MethodToDoWork;
    bw.RunWorkerCompleted += (sender, args) => MethodToUpdateControl;
    bw.RunWorkerAsync();
    

    This should get you going in the right direction.

    Edit: working sample

    public List<string> MyList { get; set; }
    
    private void button1_Click( object sender, EventArgs e )
    {
        MyList = new List<string>();
    
        var bw = new BackgroundWorker();
        bw.DoWork += ( o, args ) => MethodToDoWork();
        bw.RunWorkerCompleted += ( o, args ) => MethodToUpdateControl();
        bw.RunWorkerAsync();
    }
    
    private void MethodToDoWork()
    {
        for( int i = 0; i < 10; i++ )
        {
            MyList.Add( string.Format( "item {0}", i ) );
            System.Threading.Thread.Sleep( 100 );
        }
    }
    
    private void MethodToUpdateControl()
    {
        // since the BackgroundWorker is designed to use
        // the form's UI thread on the RunWorkerCompleted
        // event, you should just be able to add the items
        // to the list box:
        listBox1.Items.AddRange( MyList.ToArray() );
    
        // the above should not block the UI, if it does
        // due to some other code, then use the ListBox's
        // Invoke method:
        // listBox1.Invoke( new Action( () => listBox1.Items.AddRange( MyList.ToArray() ) ) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Windows Forms application and now I want to write some
I have developed a small windows forms application in C#. This application simply manipulates
I have a Windows Forms application, developed in C#, that would benefit from being
I have developed a Windows application where a button is there on a form
I have developed a C# Windows Forms application that runs in the background as
I have a Windows Forms application developed using C# in .NET framework 3.5, Service
On Windows 7, VB.NET Express, I have developed a simple Forms application. I don't
I have developed a Windows service using Visual Studio 2008. I want to install
I have an application which was developed under Windows, but for gcc. The code
I have a windows application that acts as a WCF Service that I developed

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.