When starting and pressing the button, the window is locked. The meaning of this mini program is to gradually change the color of the button for this, I made a delay in the cycle. Used an anonymous method created via Disspatcer .

How to block one thread without blocking another? If you do not block, the color will always be the last, that is, yellow. The question is how to display colors with a delay? As I understand my delay option is not correct.

 public partial class MainWindow : Window { private delegate void Del(); private Thread _thread; private static Brush _brush; private delegate void dell(); public MainWindow() { InitializeComponent(); } private void Webbtn_Click(object sender, RoutedEventArgs e) { Thread thread = new Thread(ColorsMove); thread.Start(); } private void ColorsMove() { Webbtn.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate { var c = MessageBox.Show("Hello ", "People", MessageBoxButton.OKCancel); if (c == MessageBoxResult.OK) { _brush = Webbtn.Background; var brushesType = typeof (Brushes); // Get all static properties var enumColor = brushesType.GetProperties(BindingFlags.Static | BindingFlags.Public); lock (enumColor) { foreach (var color in enumColor) { this.Background = (SolidColorBrush) color.GetValue(null, null); Thread.Sleep(TimeSpan.FromSeconds(2)); } } } else { Background = _brush; } }); } } } 
  • The question was corrected by me - Petr Smirnov
  • one
    you yourself block the stream for 2 seconds. Thread.Sleep(TimeSpan.FromSeconds(2)); - Vladyslav Matviienko
  • one
    The approach you have chosen is completely wrong and even erroneous. About blocking threads have already written in the comments above. Read about the animation in WPF , it is very for such purposes and created - Donil

1 answer 1

Dispatcher does not need to call the flow function for the whole body, otherwise there is no sense in it, the entire UI flow is blocked. Call only where you want to do an action in the UI. And lock is not quite clear why, if it is better to do Button_Click in order to prevent a restart, check whether _thread is initialized and its state is executed or not.

 public partial class MainWindow : Window { private delegate void Del(); private Thread _thread; private static Brush _brush; private delegate void dell(); public MainWindow() { InitializeComponent(); } private void Webbtn_Click(object sender, RoutedEventArgs e) { // Инициализировать лучше здесь в UI потоке чем доставать значение уже в новом потоке _brush = Webbtn.Background; Thread thread = new Thread(ColorsMove); thread.Start(); } private void ColorsMove() { var c = MessageBox.Show("Hello ", "People", MessageBoxButton.OKCancel); if (c == MessageBoxResult.OK) { var brushesType = typeof(Brushes); // Get all static properties var enumColor = brushesType.GetProperties(BindingFlags.Static | BindingFlags.Public); foreach (var color in enumColor) { Webbtn.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=> this.Background = (SolidColorBrush) color.GetValue(null, null))); Thread.Sleep(TimeSpan.FromSeconds(2)); } } else { Background = _brush; } } } 
  • Thank you, you are right - Petr Smirnov