使用Task.Factory更新进程内的标签

时间:2018-01-12 10:16:46

标签: c#

我有一个由表单按以下方式触发的过程:

public partial class LoadingDialog : Form
{
    public Action Worker { set; get; }
    public LoadingDialog(Action worker)
    {
        InitializeComponent();
        if (worker == null) throw new ArgumentNullException();

        Worker = worker;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Task.Factory
           .StartNew(Worker)
           .ContinueWith(t => 
           { 
              this.Close(); 
           }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

可以看出,LoadingDialog只是一个带有标签和进度条的小对话框。

嗯,标签需要根据流程的状态进行更新。

我无法弄清楚如何更新流程中LoadingDialog内的标签(Worker)。

任何帮助?

更新

using (LoadingDialog loadingDialog = new LoadingDialog(proceso.start_process))
            {
                loadingDialog.ShowDialog();
            }

如果我将 this 作为参数传递,它会显示错误,因为它需要使用主窗体而不是using语句中使用的LoadingDialog。

1 个答案:

答案 0 :(得分:-1)

更改

Action Worker

Action<object> Worker

使用参数启动任务&#34;此&#34;

相关问题