Updating UI in C# using Timer

后端 未结 4 1132
孤独总比滥情好
孤独总比滥情好 2020-12-02 02:56

I am working on making my application that reads data from the serial port and updates a gauge on the UI more efficient and I wanted to ask for some advice on my code that

4条回答
  •  萌比男神i
    2020-12-02 03:41

    This is the second time I provide a WPF solution for a winforms problem.

    Just copy and paste my code in a file -> new project -> WPF Application and see the results for yourself.

    Also take a look at how simple this code really is (I'm using random values, so you can remove that and adapt it to your needs).

    The drawing I used (the part in XAML) is not adequate for a Gauge. I just had that Path already drawn and I'm too lazy to create a new one. You should create a new drawing (I recommend using Expression Blend). But you can see the Rotation being applied and how fast it works.

    using System;
    using System.Threading;
    using System.Windows;
    using System.ComponentModel;
    
    namespace WpfApplication4
    {
        public partial class Window2
        {
            public Window2()
            {
                InitializeComponent();
                DataContext = new ViewModel();
            }
        }
    
        public class ViewModel: INotifyPropertyChanged
        {
            private double _value;
            public double Value
            {
                get { return _value; }
                set
                {
                    _value = value;
                    NotifyPropertyChange("Value");
                }
            }
    
            private int _speed = 100;
            public int Speed
            {
                get { return _speed; }
                set
                {
                    _speed = value;
                    NotifyPropertyChange("Speed");
                    Timer.Change(0, value);
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void NotifyPropertyChange(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private System.Threading.Timer Timer;
    
            public ViewModel()
            {
                Rnd = new Random();
                Timer = new Timer(x => Timer_Tick(), null, 0, Speed);
            }
    
            private void Timer_Tick()
            {
                Application.Current.Dispatcher.BeginInvoke((Action) (NewValue));
            }
    
            private Random Rnd;
            private void NewValue()
            {
                Value = Value + (Rnd.Next(20) - 10);
            }
        }
    }
    

    XAML:

    
        
            
                
                
                
                
            
    
            
                
                    
                        
                        
                        
                        
                    
                
            
        
    
    

提交回复
热议问题