C# 安排活动的最佳方法是什么?

C# 安排活动的最佳方法是什么?,c#,wpf,dispatchertimer,C#,Wpf,Dispatchertimer,我有一个应用程序,用户可以在其中创建不同的通知,比如便笺,并设置它们的开始时间。当他按下“开始”按钮时,计时器启动,这些提醒应该在设置的时间弹出。我已经搜索了其他答案,比如,但这里的问题是通知的时间不同 那么,安排激活通知的事件的最佳方式是什么? 我可以想到两种可能的方法,一种是优点,另一种是缺点: 运行一个Dispatchermer,它每秒都会滴答一声,检查通知时间是否已到并弹出优点:单个分派器实例缺点:每秒勾选一次并检查所有通知是一项开销 为每个通知创建一个dispatchermer,让他

我有一个应用程序,用户可以在其中创建不同的通知,比如便笺,并设置它们的开始时间。当他按下“开始”按钮时,计时器启动,这些提醒应该在设置的时间弹出。我已经搜索了其他答案,比如,但这里的问题是通知的时间不同

那么,安排激活通知的事件的最佳方式是什么? 我可以想到两种可能的方法,一种是优点,另一种是缺点:

  • 运行一个
    Dispatchermer
    ,它每秒都会滴答一声,检查通知时间是否已到并弹出优点:单个
    分派器
    实例缺点:每秒勾选一次并检查所有通知是一项开销

  • 为每个通知创建一个
    dispatchermer
    ,让他们自己处理时间优点:每个计时器只鸣响一次以弹出通知缺点:定时器过多是一种开销,可能难以控制

  • 我走对了吗?从资源角度看,这两种方法中哪一种更好?还有第三种更好的方式可以让我俯瞰吗


    编辑:如果有什么不同,通知也应该在用户定义的时间后自动关闭,并定期重复用户定义的时间间隔。

    我在C#应用程序中使用了许多方法来安排事件(线程、计时器、Quartz API…),我认为Quertz.NETAPI是您会找到的最好的工具(至少对我来说是这样)。它简单易用

    您的职业类别示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    来自互联网的示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    您将在《快速艺术指南》中找到一个有用的代码示例


    关于。

    我在C#应用程序中使用了许多方法来安排事件(线程、计时器、Quartz API…),我认为Quertz.NETAPI是您会找到的最好的工具(至少对我来说)。它简单易用

    您的职业类别示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    来自互联网的示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    您将在《快速艺术指南》中找到一个有用的代码示例


    关于。

    我在C#应用程序中使用了许多方法来安排事件(线程、计时器、Quartz API…),我认为Quertz.NETAPI是您会找到的最好的工具(至少对我来说)。它简单易用

    您的职业类别示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    来自互联网的示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    您将在《快速艺术指南》中找到一个有用的代码示例


    关于。

    我在C#应用程序中使用了许多方法来安排事件(线程、计时器、Quartz API…),我认为Quertz.NETAPI是您会找到的最好的工具(至少对我来说)。它简单易用

    您的职业类别示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    来自互联网的示例:

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }
    
    // Instantiate the Quartz.NET scheduler
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler();
    
    // Instantiate the JobDetail object passing in the type of your
    // class. Your class needs to implement a IJob interface
    var job = new JobDetail("job1", "group1", typeof(HelloJob));
    
    // Instantiate a trigger using the basic cron syntax.
    // Example : run at 1AM every Monday - Friday.
    var trigger = new CronTrigger(
        "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
    
    // Add the job to the scheduler
    scheduler.AddJob(job, true);
    scheduler.ScheduleJob(trigger);
    
    您将在《快速艺术指南》中找到一个有用的代码示例


    注意。

    如果通知系统将在单个进程内使用,请继续使用单个调度程序计时器。确保将调度程序计时器设置为接近通知。每次创建新通知或计时器命中时,将时间更改为下一个最近的通知

    这样可以避免每次处理。 例如:有人创建通知点计时器的第一次时间。如果有人在第一次点击之前创建另一个通知,请将计时器更改为第二次。如果第二次是在第一次之后,请在调度第一次通知回调后更改计时器。如果是线程化的,则可能需要努力确保线程安全


    如果在整个进程中需要通知,请使用windows任务计划程序,它已经知道如何运行计时器并及时调用我们的代码。您可能需要使用某种IPC(WCF net.pipe、msmq等)要实现通知。

    如果通知系统将在单个进程内使用,请继续使用单个调度程序计时器。确保调度程序计时器设置为接近通知。每次创建新通知或计时器命中时,将时间更改为下一个最近的通知

    这样可以避免每次处理。 例如:有人创建通知点计时器的第一次时间。如果有人在第一次点击之前创建另一个通知,请将计时器更改为第二次。如果第二次是在第一次之后,请在调度第一次通知回调后更改计时器。如果是线程化的,则可能需要努力确保线程安全


    如果在整个进程中需要通知,请使用windows任务计划程序,它已经知道如何运行计时器并及时调用我们的代码。您可能需要使用某种IPC(WCF net.pipe、msmq等)要实现通知。

    如果通知系统将在单个进程内使用,请继续使用单个调度程序计时器。确保调度程序计时器设置为接近通知。每次创建新通知或计时器命中时,将时间更改为下一个最近的通知

    这样可以避免每次处理。 例如:有人创建通知点计时器的第一次时间。如果有人在第一次点击之前创建另一个通知,请将计时器更改为第二次。如果第二次是在第一次之后,请在调度第一次通知回调后更改计时器。如果是线程化的,则可能需要努力确保线程安全


    如果在整个进程中需要通知,请使用windows任务计划程序,它已经知道如何运行计时器并及时调用我们的代码。您可能需要使用某种IPC(WCF net.pipe、msmq等)以实现通知。

    如果通知系统将在单个进程内使用,请继续使用单个调度程序计时器。确保调度程序计时器已启动