相关疑难解决方法(0)

如何从WPF gui运行异步任务并与之交互

我有一个WPF GUI,我想按下一个按钮来启动一个长任务,而不会在任务期间冻结窗口.当任务正在运行时,我想获得有关进度的报告,我想在我选择的任何时候添加另一个按钮来停止任务.

我无法想出使用async/await/task的正确方法.我不能包括我尝试的所有东西,但这就是我现在拥有的东西.

一个WPF窗口类:

public partial class MainWindow : Window
{
    readonly otherClass _burnBabyBurn = new OtherClass();
    internal bool StopWorking = false;

    //A button method to start the long running method
    private async void Button_Click_3(object sender, RoutedEventArgs e)
    {   
        Task burnTheBaby = _burnBabyBurn.ExecuteLongProcedureAsync(this, intParam1, intParam2, intParam3);

        await burnTheBaby;
    }

    //A button Method to interrupt and stop the long running method
    private void StopButton_Click(object sender, RoutedEventArgs e)
    {
        StopWorking = true;
    }

    //A method to allow the worker method to call …
Run Code Online (Sandbox Code Playgroud)

c# wpf asynchronous

44
推荐指数
4
解决办法
5万
查看次数

使用async await仍会冻结GUI

我想在单独的线程中处理长时间运行的操作,并使用async/await模式将控制返回到GUI线程,如下所示:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await Test();
    txtResult.Text = "Done!";
}

private Task Test()
{
    Thread.Sleep(3000);
    return Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)

问题是,它无论如何都会冻结GUI 3秒钟(在3秒后显示Done!之前它会变得无法响应).我究竟做错了什么?

编辑:我试图取代以下逻辑:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var thread = new Thread(() => Test(Callback));
    thread.Start();
}

private void Callback()
{
    Dispatcher.Invoke(() =>
        txtResult.Text = "Done!");
}

private void Test(Action callback)
{
    Thread.Sleep(3000); //long running operation, not necessarily pause
    callback();
}
Run Code Online (Sandbox Code Playgroud)

在实际项目中,我有不同的长时间运行逻辑而不仅仅是Sleep,它仍然会冻结GUI,因此用Task.Delay替换它并不能解决任何问题.此外,我不明白为什么你应该使用另一个命令睡眠?async/await设计需要什么?

c# user-interface asynchronous async-await

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

asynchronous ×2

c# ×2

async-await ×1

user-interface ×1

wpf ×1