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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:18:32+00:00 2026-06-15T08:18:32+00:00

I am learning how Task works. I have always used the BackgroundWorker class or

  • 0

I am learning how Task works. I have always used the BackgroundWorker class or the
Threading.Thread class so I wanted to taste Task Factory.

In the following example I am building, I recover a

 Task<List<People>>

And assign it a “ContinueWith” that I expect to be called when the task has finished.

I have done some previous examples that were working awesome but this one freeze my

Main form UI when the application is launched. (I can not see difference with the
other examples I did who doesn’t).

Do you know guys why my Main UI freezes?

PS: I know I declare

System.Threading.Tasks.TaskScheduler scheUI

on the Task declaration. I’m not using right now but I will later.

Thanks in advance:

Here is the code:

 private void PopulateList(List<People> list)
            {
                if (listBox1.InvokeRequired)
                { 
                 listBox1.Invoke((MethodInvoker) delegate {

                     foreach (People item in list)
                    {
                        listBox1.Items.Add(item.name + " " + listBox1.Items.Add(item.surname));   
                    }

                 });
            }
        }



private Task<List<People>> GetPeopleListAsync(int PeopleNuber, int delaysecs)
        { 
         TaskScheduler scheUI = TaskScheduler.FromCurrentSynchronizationContext();
            return Task<List<People>>.Factory.StartNew( () =>
                {
                    List<People> listado = new List<People>();

                    for (int i = 0; i < PeopleNuber; i++)
                    {
                        People people = new People
                        {
                            name = "Name" + i.ToString(),
                            surname = "Surname " + i.ToString()
                        };

                        Thread.Sleep(delaysecs * 200);
                        listado.Add(people);
                    }

                    return listado;


                },CancellationToken.None,
                 TaskCreationOptions.None,scheUI);

        }


 void Form1_Load(object sender, EventArgs e)
 {


            Task<List<People>> task = GetPeopleListAsync(5, 10);
            task.ContinueWith(p => PopulateList(task.Result));
 }
  • 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-15T08:18:32+00:00Added an answer on June 15, 2026 at 8:18 am

    You are synchronizing the first task with the UI thread using:

    System.Threading.Tasks.TaskScheduler scheUI = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
    

    Remove the scheUI scheduler from the first task:

    return System.Threading.Tasks.Task<List<People>>.Factory.StartNew( () =>
                {
                    List<People> listado = new List<People>();
    
                    for (int i = 0; i < PeopleNuber; i++)
                    {
                        People people = new People
                        {
                            name = "Name" + i.ToString(),
                            surname = "Surname " + i.ToString()
                        };
    
                        System.Threading.Thread.Sleep(delaysecs * 200);
                        listado.Add(people);
                    }
    
                    return listado;
    
    
                });
    

    You should add the synchronization to the second task that is executed in order to update your UI:

    System.Threading.Tasks.TaskScheduler scheUI = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
    task.ContinueWith(p => PopulateList(task.Result),CancellationToken.None,
                                  TaskContinuationOptions.ExecuteSynchronously |
                                  TaskContinuationOptions.OnlyOnRanToCompletion,
                                  scheUI);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'm learning C# and I have to do a task of creating a GUI.
I have the following method that works fine. However, I think it is ugly.
I have a task, where I have to calculate optimal policy (Reinforcement Learning -
I'm learning to use google task api rest. i have succeed getting access tokken.
I have only started learning Java. My task is to create a file server
Right, day 8 learning php and I feel that I have tried to do
I just started learning Backbone.js, and have been working on (what else) a simple
I have a PROGRAMMING task (homework) to investigate brute force neural net methods. That
I have a task at work that involves converting legacy SGM files into XML.

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.