异步/等待多功能

时间:2013-09-17 12:56:35

标签: c# wpf asynchronous async-await

我正在尝试创建一个加载数据并对其执行操作的简单应用程序。所以我的想法是做这个异步。

我有3个数据源,我想加载它们async。所以例如Data1.xml,Data2.xml和Data3.xml所有文件都要加载很大,所以需要一些时间(这就是我想要异步的原因)。

因此,例如,我创建了一个包含3个文本框的窗口,这些文本框都绑定到特定属性(Text1,Text2,Text3)和一个按钮。当我点击按钮时,我想执行3个函数async(MakeText1,MakeText2,...)。我让MakeText3成为了一个很好的,所以通常我必须先看看Text3。它不起作用,我做错了什么?

private string _text1;

    public string Text1
    {
        get { return _text1; }
        set { _text1 = value;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Text1"));
        }
    }

    private string _text2;

    public string Text2
    {
        get { return _text2; }
        set
        {
            _text2 = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Text2"));
        }
    }

    private string _text3;

    public string Text3
    {
        get { return _text3; }
        set
        {
            _text3 = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Text3"));
        }
    }

    public AsyncWin()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private async Task MakeText1()
    {
        for (double i = 0; i < 7000000; i++)
        {
            _text1 = i.ToString();
        }
        Text1 = _text1;
    }
    private async Task MakeText2()
    {
        for (double i = 0; i < 3000; i++)
        {
            _text2 = i.ToString();
        }
        Text2 = _text2;
    }
    private async Task MakeText3()
    {
        for (double i = 0; i < 10; i++)
        {
            _text3 = i.ToString();
        }
        Text3 = _text3;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _text1 = "";
        _text2 = "";
        _text3 = "";
        Test();
        Console.WriteLine();
    }
    public async void Test()
    {
        MakeText1();
        MakeText2();
        MakeText3();
    }
    public event PropertyChangedEventHandler PropertyChanged;

的Xaml:

    <Grid>
    <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="181" Margin="10,19,0,0" TextWrapping="Wrap" Text="{Binding Text1}" VerticalAlignment="Top" Width="110"/>
    <TextBox x:Name="txt2" HorizontalAlignment="Left" Height="181" Margin="137,19,0,0" TextWrapping="Wrap" Text="{Binding Text2}" VerticalAlignment="Top" Width="110"/>
    <TextBox x:Name="txt3" HorizontalAlignment="Left" Height="181" Margin="276,19,0,0" TextWrapping="Wrap" Text="{Binding Text3}" VerticalAlignment="Top" Width="110"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,219,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>

1 个答案:

答案 0 :(得分:3)

  

我希望UI首先显示Text3而不是Text2

在您的代码中不会发生这种情况,因为Text1 .. Text3的setter中的人工延迟循环会同步运行,而不会让其他任务执行任务。如果用Task.Delay(...)替换这些循环,您将获得所需的效果:

private async Task MakeText1() {
    await Task.Delay(3000);
    Text1 = "3 seconds";
}
private async Task MakeText2() {
    await Task.Delay(2000);
    Text2 = "2 seconds";
}
private async Task MakeText3() {
    await Task.Delay(1000);
    Text3 = "1 second";
}