从Task.Run异步更新进度条

7

我有一种方法可以从用户信息生成软件激活密钥:

private async void GenerateAsync()
        {
            await Task.Run(() =>
            {
                var secretstring = "CustomerName >> " + txtFullname.Text + " >> " + txtproducid.Text;
                keymng = new KeyManager(secretstring);
                var dateexp = numExpdate.Value;
                if (dateexp == 0)
                {
                    dateexp = 730000;
                }

                if (cbLicenseType.SelectedIndex == 0)
                {
                    kvc = new KeyValueClass()
                    {
                        LicenseType = LicenseType.FULL,
                        Header = Convert.ToByte(9),
                        Footer = Convert.ToByte(6),
                        ProductCode = PRODUCTCODE,
                        Edition = (Edition)Enum.Parse(typeof(Edition), cbEdition.SelectedText),
                        Version = 1,
                        Expiration = DateTime.Now.AddDays(Convert.ToInt32(dateexp))
                    };

                    if (!keymng.GenerateKey(kvc, ref productkey))
                    {
                        //Handling Error
                    }
                }
                else
                {
                    kvc = new KeyValueClass()
                    {
                        LicenseType = LicenseType.TRIAL,
                        Header = Convert.ToByte(9),
                        Footer = Convert.ToByte(6),
                        ProductCode = PRODUCTCODE,
                        Edition = (Edition)Enum.Parse(typeof(Edition), cbEdition.SelectedText),
                        Version = 1,
                        Expiration = DateTime.Now.AddDays(Convert.ToInt32(dateexp))
                    };
                    if (!keymng.GenerateKey(kvc, ref productkey))
                    {
                        //Handling Error
                    }
                }
            });

        }

我有一个进度条和一个需要异步运行的方法,希望能够更新进度条。如何使用async-await方法更新UI?BackgroundWorker是一种替代方案吗?

2个回答

23

您应该使用更现代的 IProgress<T>/Progress<T> 类型:

var progress = new Progress<int>(value => { progressBar.Value = value; });
await Task.Run(() => GenerateAsync(progress));

void GenerateAsync(IProgress<int> progress)
{
  ...
  progress?.Report(13);
  ...
}

这比Invoke更好,因为它不会将您的业务逻辑(GenerateAsync)与特定的 UI 或任何 UI 绑定在一起。


6
我在Winforms中使用这种方法:
progressBar1.Invoke((Action)(() => progressBar1.Value=50))

很好的回答,虽然我不确定你是否需要将它转换为动作。应该可以像这样实现 progressBar1.Invoke(() => { progressBar1.Value = 50 }); - Nico
@Nico 好的,我会尝试不使用类型转换。 - Bali C
好的回答!我会试一下。 - alextran
如果不可能的话请告诉我 @BaliC :) 我已经有一段时间没有使用过C#了。 - Nico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接