Avatar of whorsfall
whorsfall
Flag for Australia asked on

C# Await issue

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

C#

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck