Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag 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.

User generated image
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

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial