1

データをロードしてアクションを実行する単純なアプリケーションを作成しようとしています。だから私の考えは、これを非同期にすることでした。

3 つのデータ ソースがあり、それらを非同期でロードしたいと考えています。たとえば、Data1.xml、Data2.xml、および Data3.xml のすべてのファイルは、ロードするのに非常に大きいため、時間がかかります (そのため、非同期が必要です)。

たとえば、特定のプロパティ (Text1、Text2、Text3) とボタンにすべてバインドする 3 つの Textbox を含むウィンドウを作成しました。ボタンをクリックすると、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>
4

1 に答える 1