We help IT Professionals succeed at work.
Get Started

C# Await issue

whorsfall
whorsfall asked
on
55 Views
Last Modified: 2020-01-27
Hi,

I am doing some testing with Await in WinForms.

What I am puzzled at the behaviour is why when the code
runs the progress bar does not stay in sync with the textbox.

Please see my screen shot plus my code,

What am i doing wrong and why is it behaving the way it is?

Thanks,

Ward.

Screen shot of issue.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winforms_Task_Test1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private async void button_Go_Click(object sender, EventArgs e)
        {
            TestClass item = new TestClass();

            int limit = 10;

            item.Init(textBox_Status, progressBar_Progress, limit, 100);

            for (int i=1;i<=limit;i++)
            {
                await item.Process(i);
            }

        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winforms_Task_Test1
{
    class TestClass
    {
        public Control Text_control;
        public ProgressBar Progress_control;
        public int delay;
        public int max_value;
        public void SetControlThreadSafe(Control control, Action<object[]> action, object[] args)
        {
            if (control.InvokeRequired)
            {
                try
                {
                    control.Invoke(new Action<Control, Action<object[]>, object[]>(SetControlThreadSafe), control, action, args);
                }
                catch
                {
                }
            }
            else action(args);
        }

        public void Init(Control textbox_control, ProgressBar progressbar_control, int limit, int delay_ms)
        {
            Text_control = textbox_control;
            Progress_control = progressbar_control;

            SetControlThreadSafe(Text_control, (arg) =>
            {
                Progress_control.Value = 0;
                Progress_control.Maximum = limit;
                // Progress_control.Refresh();
            }, null);

            SetControlThreadSafe(Text_control, (arg) =>
            {
                Text_control.Text = "";
                // Text_control.Refresh();
            }, null);

            delay = delay_ms;
            max_value = limit;
        }

        public async Task Process(int counter)
        {
            SetControlThreadSafe(Progress_control, (arg) =>
            {
                Progress_control.Value = counter;
                Progress_control.Refresh();
                }, null);

            SetControlThreadSafe(Text_control, (arg) =>
            {
                Text_control.Text = string.Format("Counter: {0}", counter);
                Text_control.Refresh();
                }, null);


            await Task.Delay(delay);

        }

    }


}

Open in new window

Comment
Watch Question
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE