Application scheduling in wpf

I am trying to create a WPF multimedia application to run audio files using Media Element. I have succeeded. But I want to schedule the playback of songs that I have selected over a period of time. Speak 10 hours every day or every hour, etc.

What is the best way to do this?

First, I think about doing this with a timer. But this makes my code complicated because I have to play several songs at different intervals set by the user.

I recently learned about the "Task Scheduler" and I ran my sample code [open notepad] and it works great.

  using (TaskService ts = new TaskService())
  {
     // Create a new task definition and assign properties
     TaskDefinition td = ts.NewTask();
     td.RegistrationInfo.Description = "Does something";

     // Create a trigger that will fire the task at this time every other day
     td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

     // Create an action that will launch Notepad whenever the trigger fires
     td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

     // Register the task in the root folder
     ts.RootFolder.RegisterTaskDefinition(@"Test", td);

     // Remove the task we just created
     ts.RootFolder.DeleteTask("Test");
  }

, ? ExecAction, SendEmail, ShowMessage ComHandlerAction .

? , , .

+4
1

-, . DispatcherTimer, Interval :

private DispatcherTimer mediaPlayerTimer = null;

...

mediaPlayerTimer = new DispatcherTimer();
mediaPlayerTimer.Interval = YourFirstDateTime.Subtract(DateTime.Now);
mediaPlayerTimer.Tick += MediaPlayerTimer_Tick;
mediaPlayerTimer.Start();

...

private void MediaPlayerTimer_Tick(object sender, EventArgs e)
{
    mediaPlayerTimer.Stop();
    // Load next audio file and play
    // Remove YourFirstDateTime from your collection
    // Set YourFirstDateTime = the next item from the collection
    mediaPlayerTimer.Interval = YourFirstDateTime.Subtract(DateTime.Now);
    mediaPlayerTimer.Start();
}
+6

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


All Articles