question

Sarah-3412 avatar image
0 Votes"
Sarah-3412 asked HuiLiu-MSFT edited

How to execute specified function every defined date and time

I have developed a WPF app with multiple Views and ViewModels. The app stores a lot of data and information. This is not a problem up to here. I have now written a function "CreateReport" that generates a report for me. The report should be generated automatically in the background (without user intervention/no button click) on the following dates and times:
- Every day at 5pm
- Every week on friday at 5pm
- Every month on the last Friday at 5pm
- Every year on the last friday at 5pm

Is this possible with WPF using MVVM? Does anyone have experience and do a demo? Unfortunately I have not found anything that could help me.


P.s. the other functionalities of the app shall not be affected.

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT edited

DispatcherTimer class is recommended in wpf.

 using System;
 using System.Windows;
 using System.Windows.Threading;
    
 namespace ExecuteFunctionOnSpecialTime
 {
   public partial class MainWindow : Window
   {
       
     DispatcherTimer dispatcherTimer { get;set;}
     public MainWindow()
     {
       InitializeComponent();
       dispatcherTimer = new DispatcherTimer();
       dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
       dispatcherTimer.Interval = new TimeSpan(24, 0, 0);
       dispatcherTimer.Start();
     }
     private void dispatcherTimer_Tick(object sender, EventArgs e)
     {
       Function();
     }
     public void Function()
     {
       MessageBox.Show("hello");
     }
   }
 }

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. 

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HuiLiu-MSFT , Thanks but it is not exactly what I want to do and what I asked.
1) Your code works only when the app is running, otherwise not. My goal is: even if the app is not running, this should be detected at the next startup and the function "CreateReport" should be called when the defined time is expired.
2) Your code is for code behind. I don't see how and where to implement this using MVVM pattern. A demo using MVVM pattern was a part of my main question.

0 Votes 0 ·

Hi Sarah,
you can create a list of scheduled task times. This list is saved at the end of the program, e.g. in the settings. When the program starts, the list is loaded and all unscheduled tasks that should be processed during the pause are executed. For the next pending task, DispatcherTimer is activated with the difference time.

0 Votes 0 ·
Sarah-3412 avatar image Sarah-3412 PeterFleischer-3316 ·

Hi @PeterFleischer-3316 , I think the idea is good. But unfortunately I have no idea how to implement it. Can you please make a demo and show how to save it in Settings?

Very important, it should work in MVVM pattern.

0 Votes 0 ·
Show more comments