Task in Model not updating ui - MVVM Approach

Kalpana 286 Reputation points
2022-09-16T02:01:50.547+00:00

Hi

I am at lost again, why is that the task in the model does not update the UI whereas without the task, the ui gets updated fine, I believe it is something to do with threading, my code below. I remember doing something similar with the task located at the view model and that was executing fine, however, I thought I should do this in my model instead of view model.

public class Machine : INotifyPropertyChanged  
    {  
        public Machine()  
        {  
              
            Dtimer = new DispatcherTimer();  
               CallDispatcherTimer();  
            
  
        }  
        private DateTime _mchCurrentTime;  
  
        public DateTime MchCurrentTime  
        {  
            get  
            {  
                 
                return _mchCurrentTime;  
            }  
            set  
            {  
                if (_mchCurrentTime != value)  
                {  
                    _mchCurrentTime = value;  
                    OnPropertyChanged("MchCurrentTime");  
                }  
  
            }  
        }  
  
  
        
          
  
        public DispatcherTimer Dtimer { get; set; }  
        public void CallDispatcherTimer()  
        {  
             
            Dtimer.Interval = TimeSpan.FromSeconds(1);  
            Dtimer.Tick += ExecuteList;  
            Dtimer.Start();  
        }  
  
  
      
        
  
        public void RetrieveCurrentDateTime()  
        {  
            MchCurrentTime = DateTime.Now;  
  
        }  
  
        public async void ExecuteList(object sender, EventArgs e)  
        {  
              
  
            Task task = new Task(new Action(RetrieveCurrentDateTime));  
            task.Start();  
            await task;  
  
        }  
  
  
  
  
        public event PropertyChangedEventHandler PropertyChanged;  
  
        public void OnPropertyChanged(string propertyName)  
        {  
            PropertyChangedEventHandler ph = PropertyChanged;  
            if (ph != null)  
                ph(this, new PropertyChangedEventArgs(propertyName));  
        }  
    }  

In my viewmodel, I am just creating an instance of the Machine.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,246 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-09-16T05:51:05.273+00:00

    I used the code below with your Model code and it should work fine. If there are any questions please let me know.
    Xaml:

    <Window.DataContext>  
            <local:MachineViewModel/>  
        </Window.DataContext>  
        <StackPanel Orientation="Horizontal" Margin="250,0,0,0">  
            <Label Content="Date" Width="45" Height="30"/>  
            <TextBox x:Name="txtboxdate" Width="100" Height="20" IsReadOnly="True" Background="#bc9d1b" Text="{Binding Path=Mch.MchCurrentTime, StringFormat=MM/dd/yyyy, Mode=OneWay}"/>  
            <Label Content="Time" Width="45" Height="30" Margin="40,0,0,0"/>  
            <TextBox x:Name="txtboxtime" Width="100" Height="20" IsReadOnly="True" Background="#bc9d1b" Text="{Binding Path=Mch.MchCurrentTime, StringFormat=hh:mm:ss.fff tt, Mode=OneWay}"/>  
        </StackPanel>  
    

    ViewModel:

     public class MachineViewModel   
        {  
      
            public MachineViewModel()  
            {  
                _mch = new Machine();  
      
            }  
      
            private Machine _mch;  
      
            public Machine Mch  
            {  
                get  
                {  
                    return _mch;  
                }  
                set  
                {  
                    _mch = value;  
                }  
            }  
        }  
    

    The result:
    241771-%E5%8A%A8%E7%94%BB.gif

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.