none
Update UI from Task.Run freezing application RRS feed

  • Question

  • I am learning Task usage and async await usage. so i write a small program which will update my label value in loop. when i run application then application is getting freeze and label value change is not showing. so please guide me where i made the mistake. here is my code example.

    private async void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                var count = 0;
    
                await Task.Run(() =>
                {
                    for (var i = 0; i <= 5000000; i++)
                    {
                        count = i;
                        BeginInvoke((Action)(() =>
                        {
                            label1.Text = i.ToString();
    
                        }));
                    }
                });
    
                label1.Text = @"Counter " + count;
                button1.Enabled = true;
            }
    Saturday, December 22, 2018 6:43 PM

Answers

  • problem solved i have done the job this way

    private async void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                var count = 0;

                await Task.Run(() =>
                {
                    for (var i = 0; i <= 500; i++)
                    {
                        count = i;
                        BeginInvoke((Action)(() =>
                        {
                            label1.Text = i.ToString();

                        }));
                       Thread.Sleep(100);
                    }
                });

                label1.Text = @"Counter " + count;
                button1.Enabled = true;
            }

    • Marked as answer by Sudip_inn Sunday, December 23, 2018 5:26 PM
    Sunday, December 23, 2018 5:11 PM

All replies

  • Try this adjustment:

       var r = BeginInvoke( . . . );

       r.AsyncWaitHandle.WaitOne();

    Or use Invoke instead of BeginInvoke.

    Saturday, December 22, 2018 7:05 PM
  • please give me full code. your snippet is not clear.
    Sunday, December 23, 2018 9:26 AM
  • problem solved i have done the job this way

    private async void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                var count = 0;

                await Task.Run(() =>
                {
                    for (var i = 0; i <= 500; i++)
                    {
                        count = i;
                        BeginInvoke((Action)(() =>
                        {
                            label1.Text = i.ToString();

                        }));
                       Thread.Sleep(100);
                    }
                });

                label1.Text = @"Counter " + count;
                button1.Enabled = true;
            }

    • Marked as answer by Sudip_inn Sunday, December 23, 2018 5:26 PM
    Sunday, December 23, 2018 5:11 PM
  • see my code and give me full code.
    Saturday, January 12, 2019 5:37 PM