1

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

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

因此,例如,我制作了一个带有 3 个文本框的窗口,这些文本框都绑定到特定属性(Text1、Text2、Text3)和一个按钮。当我单击按钮时,我想异步执行 3 个函数(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;

xml:

    <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>
4

1 回答 1

3

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

这不会在您的代码中发生,因为在 setter for Text1..中的人工延迟循环会Text3同步运行,而不会让其他任务继续执行。如果将这些循环替换为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";
}
于 2013-09-17T13:02:35.740 回答