BackgroundWorker完成后停止故事板

时间:2015-09-23 12:06:54

标签: c# model-view-controller backgroundworker

调用Method ExportStarts()时,我想开始动画。然后我在逻辑单元(Manager.StartExport())中调用另一个方法,在那里我使用Background Worker进行操作。完成后,我想回到视图并停止动画。我怎么能这样做?

View.xaml.cs

if (...)
{
 storyboard.Begin();                 
 List<TaskResult> Results = manager.StartExport();
 storyboard.Stop();
}

manager.StartExport()

public static List<TaskResult> StartExport()
    {
        bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(worker_Do);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunCompleted);
        bw.RunWorkerAsync();

        return Results;
    }

提前致谢

1 个答案:

答案 0 :(得分:0)

你想在worker_RunCompleted yes之后停止故事板吗?

检查TaskCompletionSource :)

如果我理解正确 - 这会有所帮助。

_storyboard.Stop();中设置_storyboardTaskCompletionSource的结果后,

RunWorkerCompleted会加注。

private TaskCompletionSource<bool> _storyboardTaskCompletionSource;
Storyboard _storyboard = new Storyboard();

private async Task InitAsync()
{
    _storyboardTaskCompletionSource = new TaskCompletionSource<bool>();
    _storyboard.Begin();

    StartProgress();

    await _storyboardTaskCompletionSource.Task;

    _storyboard.Stop();
}

public void StartProgress()
{
    var bw = new BackgroundWorker();
    bw.DoWork += DoWork;
    bw.RunWorkerCompleted += RunWorkerCompleted;
    bw.RunWorkerAsync();
}

private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    _storyboardTaskCompletionSource.SetResult(true);
}

private void DoWork(object sender, DoWorkEventArgs e)
{
    //logic
}