5

テキストボックスを使用して、タスクの完了を表示しようとしています。基本的に、コンソールアプリケーションが何が起こっているかを表示する方法と同じです。

ただし、テキストボックス内のテキストはWindow_Loaded_1が完了した後にのみ更新され、リアルタイムではなくすべてのテキストが表示されます。

xamlコード:

<Window x:Class="timelineTesting.Windows.CreateNewProject"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CreateNewProject" Height="300" Width="579" Loaded="Window_Loaded_1">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox Text="{Binding Path=LogData, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

C#コード:

public partial class CreateNewProject : Window, INotifyPropertyChanged
{
    private string _data;
    public String LogData
    {
        get
        {
            return _data;
        } 
        set
        {
            _data = value;
            OnPropertyChanged("LogData");
        }
    }

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

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        Task t = new Task(() => Directory.CreateDirectory(this.ProjectName));

        LogData+="Starting new project creation...." + Environment.NewLine;
        LogData += "Creating project directory '" + ProjectName + "'....";
        try
        {
            t.Start();
            t.Wait();
        }
        catch (Exception ex)
        {
            LogData += "Error:" + Environment.NewLine;
            LogData += ex.InnerException.ToString();
        }

        LogData+= "Done!" + Environment.NewLine;

        t = new Task(() => File.Copy(this.VideoFilePath, newVideoPath));
        LogData+= "Copying video file to project directory....";
        try
        {
            t.Start();
            t.Wait();
        }
        catch (Exception ex)
        {
            LogData+= "Error:" + Environment.NewLine;
            LogData+= ex.InnerException.ToString();
        }

        LogData+= "Done!" + Environment.NewLine;
        // many more tasks
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
4

3 に答える 3

3

t.Wait()ブロッキングコールです。つまり、実際にはマルチスレッドを行っていません。タスクを開始し、完了するまで待ちます。あなたがすべきことはawait、タスクを完了することです。

于 2012-11-08T19:52:06.637 に答える
2

代わりに、同時ステータスでテキストボックスを更新できるProgressChangedイベントを持つバックグラウンドワーカーを使用してください。

記事を参照してください:C#WPF:スレッド化、コントロールの更新、ステータスバー、キャンセル操作の例オールインワンで例を示します。

于 2012-11-08T19:55:02.633 に答える
2

使用await:

private async void Window_Loaded_1(object sender, RoutedEventArgs e)
{
  Task t = Task.Run(() => Directory.CreateDirectory(this.ProjectName));
  LogData += "Starting new project creation...." + Environment.NewLine;
  LogData += "Creating project directory '" + ProjectName + "'....";
  try
  {
    await t;
  }
  catch (Exception ex)
  {
    LogData += "Error:" + Environment.NewLine;
    LogData += ex.ToString();
  }

  LogData += "Done!" + Environment.NewLine;

  t = Task.Run(() => File.Copy(this.VideoFilePath, newVideoPath));
  LogData += "Copying video file to project directory....";
  try
  {
    await t;
  }
  catch (Exception ex)
  {
    LogData += "Error:" + Environment.NewLine;
    LogData += ex.ToString();
  }

  LogData += "Done!" + Environment.NewLine;

  // many more tasks
}
于 2012-11-08T20:03:19.097 に答える