troubleshooting Question

C# Await issue

Avatar of whorsfall
whorsfallFlag for Australia asked on
C#
1 Comment1 Solution58 ViewsLast Modified:
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);

        }

    }


}
ASKER CERTIFIED SOLUTION
it_saige
Developer
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros