【问题标题】:TimeTrigger/Scheduler in Windows 10 (UWP) for less than 15 minutesWindows 10 (UWP) 中的 TimeTrigger/Scheduler 少于 15 分钟
【发布时间】:2015-10-02 02:33:46
【问题描述】:

我发现使用 TimeTrigger 是 Windows 10 (UWP) 上计划后台任务的方式。但是看起来我们需要给出的最小数字是 15 分钟。只是想知道,Windows 10 的警报和提醒如何工作,即使我们安排它在接下来的 1 分钟运行。

我正在寻找一种应在特定时间(包括不到 15 分钟)内触发任务的解决方案。

任何人都可以对此有所了解吗?

【问题讨论】:

  • 报警和提醒不同。他们不需要TimeTriggerstackoverflow.com/questions/31740884/…
  • 我浏览了 Github 上的示例代码,发现 Alarm 和其他 toast 是基于按钮单击显示的。您知道如何将其附加到即使前台应用程序未运行也可以工作的计时器?我不确定它是如何按预定时间运行的。正如杰弗里指出的那样,这是由“ToastNotificationManager.CreateToastNotifier().AddToSchedule”完成的吗? Github Windows 10 示例上的任何代码?
  • 知道如何以这种类似的方式调度运行代码块(根据用户选择的时间,可能少于 15 分钟)?

标签: scheduler uwp


【解决方案1】:

Alarm & Clock 应用程序正在使用 Scheduled Toast Notification 而不是后台任务。

这是在 10 秒内安排敬酒的示例代码。

namespace UWPApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private Random random = new Random((int)DateTime.Now.Ticks);

        const string TOAST = @"
<toast>
  <visual>
    <binding template=""ToastGeneric"">
      <text>Sample</text>
      <text>This is a simple toast notification example</text>
      <image placement = ""AppLogoOverride"" src=""oneAlarm.png"" />
    </binding>
  </visual>
  <actions>
    <action content = ""check"" arguments=""check"" imageUri=""check.png"" />
    <action content = ""cancel"" arguments=""cancel""/>
  </actions>
  <audio src =""ms-winsoundevent:Notification.Reminder""/>
</toast>";


        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var when = DateTime.Now.AddSeconds(10);

            var offset = new DateTimeOffset(when);

            Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();

            xml.LoadXml(TOAST);

            ScheduledToastNotification toast = new ScheduledToastNotification(xml, offset);

            toast.Id = random.Next(1, 100000000).ToString();

            ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
        }
    }
}

【讨论】:

  • 我明白了。那么,这会在后台运行吗?即使前台应用程序没有运行,AddToSchedule 也会运行吗?有什么方法可以在 10 分钟内执行一段代码?
  • 知道如何以这种类似的方式调度运行代码块(根据用户选择的时间,可能少于 15 分钟)?
  • 这实际上回答了我关于警报及其时间的问题。标记为答案。
猜你喜欢
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 2021-01-08
相关资源
最近更新 更多