提问者:小点点

异步/等待多功能


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

我有3个数据源,我想异步加载它们。例如data1.xml、data2.xml和data3.xml所有文件加载起来都相当大,所以需要一些时间(这就是为什么我想要异步的原因)。

例如,我创建了一个包含3个文本框的窗口,这些文本框都绑定到一个特定的属性(Text1、Text2、Text3)和一个按钮。当我点击按钮时,我想异步执行3个函数(MakeText1、MakeText2、...)。我将MakeText3设置为fastes,所以通常我必须首先查看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个答案

匿名用户

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

在您的代码中不会发生这种情况,因为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";
}

相关问题