简体   繁体   English

异步等待停止... C# WPF

[英]Async Await Stops... C# WPF

I have a function that gets a Func as a parameter and executes it inside of a task in a async method in order to display indeterminate progress bar while the function is working....我有一个函数,它获取一个 Func 作为参数并以异步方法在任务内部执行它,以便在函数工作时显示不确定的进度条....

private async void Invoke(Func<string> function)
{
       if(calculation?.Status == TaskStatus.Running) await calculation;
       calculation = Task.Run(function);
       InvokeA(() => prg.Height = double.NaN);
       InvokeA(() => prg.Visibility = Visibility.Visible);
       Result = await calculation;
       InvokeA(() => prg.Visibility = Visibility.Hidden);
       InvokeA(() => prg.Height = 0);
}

The problem is when ever the function reaches the:问题是当函数达到:

Result = await Calculation;

it sort of stops... it never gets to actually setting the value or closing the progress bar.它有点停止......它永远不会实际设置值或关闭进度条。

Invoke Is Called from a Textbox.KeyDown Method if the key is Enter:如果键为 Enter,则从 Textbox.KeyDown 方法调用 Invoke:

if (SpecialCommands.DoesExist(Text))
    {
          Invoke(() => SpecialCommands.Invoke(Parameter));
          if (!string.IsNullOrEmpty(Result)) Text += $"   --->   {Result}";
     }

variables definition:变量定义:

calculation = Task<string>
Result = string
prg = ProgressBar
Parameter = string that isn't connected to the UI

I've created a standard new WPF application project with the following XAML我使用以下 XAML 创建了一个标准的新 WPF 应用程序项目

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Canvas>
        <Button x:Name="button" Content="Button" Canvas.Left="62" Canvas.Top="40" Width="75" Click="button_Click"/>
        <ProgressBar x:Name="prg" Height="23" Canvas.Left="62" Canvas.Top="265" Width="347" IsIndeterminate="True"/>
    </Canvas>
</Window>

and code behind和后面的代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    Task<string> calculation;
    string Result;
    string Text;

    private async void button_Click(object sender, RoutedEventArgs e)
    {
        await Invoke(() => Foo());
        if (!string.IsNullOrEmpty(Result)) Text += $"   --->   {Result}";
    }

    private async Task Invoke(Func<string> function)
    {
        if (calculation?.Status == TaskStatus.Running) await calculation;
        calculation = Task.Run(function);
        InvokeA(() => prg.Height = double.NaN);
        InvokeA(() => prg.Visibility = Visibility.Visible);
        Result = await calculation;
        InvokeA(() => prg.Visibility = Visibility.Hidden);
        InvokeA(() => prg.Height = 0);
    }

    private void InvokeA(Action a) { a(); }

    static string Foo()
    {
        Thread.Sleep(5000);
        return "Bar";
    }
}

and it's working as expected.它按预期工作。 So the problem is somewhere else from what you provided.所以问题出在你提供的其他地方。

EDIT Based on your comment, it's worth trying the following, because that was the incorrect part of your code:编辑根据您的评论,值得尝试以下操作,因为那是您的代码不正确的部分:

First, change the signature of the Invoke method to be首先,将Invoke方法的签名更改为

private async Task Invoke(Func<string> function)

and then use然后使用

await Invoke(() => Foo());
if (!string.IsNullOrEmpty(Result)) Text += $"   --->   {Result}";

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM