首页 > 解决方案 > Quartz.NET 和 WPF

问题描述

我被困住了,我需要你的帮助。我正在开发一个 WPF 应用程序,它从数据库中读取数据,并将该数据存储到 ObservableCollecion 中。ObservableCollection 绑定到 UI 上的 TreeView (HierarchicalDataTemplate)。这样,当 ObservableCollection 中的属性更新时,UI 也会更新。应用程序需要在准确的预定时间对后台数据进行一些处理,为此我决定使用 Quartz.NET。对于我需要的东西,它似乎非常准确。我的问题是我无法弄清楚如何从 Quartz 的 IJob 类中更新 ObservableCollection。我希望您可以通过给我一些指示来帮助我。以下是我的代码示例。我尽量缩短,

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        this.MyCollection = new ObservableCollection<MyData>();

        BindingOperations.CollectionRegistering += BindingOperations_CollectionRegistering;

        // Bind TreeView to ObservableCollection MyCollection
        myTreeView.ItemsSource = MyCollection;

        // I have methods that get data from a DB and populate the ObservableCollection MyCollection
        GetDataFromDB();

       // Initialize the Quartz.NET scheduler
       RunQueuedEvents().GetAwaiter().GetResult();
    }

    public void BindingOperations_CollectionRegistering(object sender, CollectionRegisteringEventArgs e)
    {
        if (e.Collection == MyCollection)
        {
            BindingOperations.EnableCollectionSynchronization(MyCollection, lockMyCollection);
        }
    }

    public ObservableCollection<MyData> MyCollection { get; }

    // object used to lock the ObservableCollection MyCollection
    private object lockMyCollection = new object();

    private async Task RunQueuedEvents()
    {
        try
        {
            // Grab the Scheduler instance from the Factory
            NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } };
            StdSchedulerFactory factory = new StdSchedulerFactory(props);
            IScheduler scheduler = await factory.GetScheduler();

            // Start the Scheduler instance
            await scheduler.Start();

            // Define the Job
            IJobDetail job = JobBuilder.Create<MyJob>()
                    .WithIdentity("job1", "group1")
                    .UsingJobData("MyCommand", "DO IT 13423")
                    .Build();

            // Define the Trigger
            ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .StartAt(myStartTime)// some Time I want it to trigger at
                    .Build();


             // Tell Quartz to schedule the Job using our Trigger
             await scheduler.ScheduleJob(job, trigger);
            }
        }
        catch (SchedulerException se)
        {
        }
    }
}

我的 Quartz.NET 工作如下所示:

public class MyJob : IJob
{
    public string MyCommand { private get; set; }

    public async Task Execute(IJobExecutionContext context)
    {
        await Task.Run(() =>
        {
            //In here I process the MyCommand
        });

        // This is where I am stuck now. How do I update the properties within the ObservableCollection, once the Task has finished running.
    }
}

标签: c#wpfquartz.net

解决方案


推荐阅读