The text field will not be updated until the method is executed.

I am trying to use a text box to show completion of tasks. In principle, how the console application will show what is happening.

However, the text in the text box is updated only after Window_Loaded_1 completes, then all the text will be displayed instead of real-time.

xaml code:

<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 # code:

 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
source share
3 answers

Use 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 } 
+1
source

t.Wait() is a blocking call. This means that you really are not doing multithreading. You start the task and wait for its completion. To complete the task you need to execute await .

+3
source

Instead, use a Background Worker that has a ProgressChanged event that can update your text box with concurrent status.

See article: C # WPF: Threading, Control Updating, Status Bar and Cancel Operations All In One example, which gives an example.

+2
source

Source: https://habr.com/ru/post/1444910/


All Articles