5

我正在尝试使用文本框来显示任务完成。基本上就像控制台应用程序如何显示正在发生的事情一样。

然而,文本框中的文本仅在 Window_Loaded_1 完成后更新,然后所有文本都会显示而不是实时显示。

xml代码:

<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:线程、控制更新、状态栏和取消操作示例 All In One,其中提供了一个示例。

于 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 回答