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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:16:31+00:00 2026-05-27T00:16:31+00:00

I have a task that might take a while to complete. I have tried

  • 0

I have a task that might take a while to complete.

I have tried a simple demo but it locks up the GUI thread for some reason. I thought the task would be asynchronous and the GUI would update whilst the task is running.

Is this possible with a Task?

    private void button5_Click(object sender, EventArgs e)
    {
        var task = Task.Factory.StartNew(() => DoSomething());
        while (!task.IsCompleted)
        {
            label1.Text += ".";
            if (label1.Text.Length == 5)
                label1.Text = ".";
        }
    }

    private void DoSomething()
    {
        Thread.Sleep(5000);
    }

UPDATE: After Damien’s answer I tried the below but the CPU ramped up

        private void button5_Click(object sender, EventArgs e)
        {
            var task = Task.Factory.StartNew(() => DoSomething());
        }
        public delegate void dgUpdateLabel();

        private void UpdateLabel()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new dgUpdateLabel(UpdateLabel), new object[] { });
            }
            else
            {
                label1.Text += ".";
                if (label1.Text.Length == 5)
                    label1.Text = ".";
            }
        }

        private void DoSomething()
        {
            var task = Task.Factory.StartNew(() => Sleep());
            while (!task.IsCompleted)
            {
                UpdateLabel();
            }
        }

        private void Sleep()
        {
            Thread.Sleep(5000);
        }

UPDATE2: I think the speed of trying to update the label is too fast for it to handle. If you put a Thread.Sleep(500) after the UpdateLabel method call it works as expected.

  • 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-27T00:16:31+00:00Added an answer on May 27, 2026 at 12:16 am

    Updating according to our chat 🙂 :

    private void Form1_Load(object sender, EventArgs e)
    {
      CancellationTokenSource cts = new CancellationTokenSource();
      Task worker = new Task(() => DoSomething());
      Task ui_updater = new Task(() => UpdateGui(CancellationToken token));
      worker.Start();
      updater.Start();
      // Worker task completed, cancel GUI updater.
      worker.ContinueWith(task => cts.Cancel());
    }
    private void DoSomething()
    {
     // Do an awful lot of work here.
    }
    private void UpdateGui(CancellationToken token)
    { 
      while (!token.IsCancellationRequested)
     {      
       UpdateLabel();
       Thread.Sleep(500);
     }
    }
    private void UpdateLabel()
    {
      if (this.InvokeRequired)
      {
       this.BeginInvoke(new Action(() => UpdateLabel()), new object[] { });
      }
        else
        {
            label1.Text += ".";
            if (label1.Text.Length >= 5)
                label1.Text = ".";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Runnable implementation that does some work which might take some time
I have a custom MSBuild task that peeks inside an assembly to get some
I have a database-synchronisation task that takes some time to process, as there are
I'm struggling with a task that could be very simple but I'm having a
I have a task that takes a rather long time and should run in
I have a Task object that has a collection of Label objects ... in
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a scheduled task that is very IO intensive (deleting hundreds of thousands
I have a Fabric task that needs to access the settings of my Django
We currently have an ant task that contains something similar to the following: <filelist

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.