c# - 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.
}
}
解决方案
推荐阅读
- node.js - Docusign:禁用预格式化样式签名并允许用户仅绘制或上传他们的签名
- bash - bash 命令替换作为单个命令执行,不被解析为 shell 语句
- .net - 如何安装支持 .NET Core 6.0 的 .NET SDK?
- ruby-on-rails - 如何打开rails控制台以访问rails 6中的多个数据库?
- azure-data-factory - Azure 数据工厂:每次成功运行后如何设置变量?
- join - 需要哈希连接清晰度
- java - 解密 SAML 2 断言
- sql - 错误:无法对包含聚合或子查询的表达式执行聚合函数
- javascript - 如何访问与 Node 中的调用模块相关的 JSON 文件?
- python-3.x - AssertionError:标签类 33 超过 ././models/yolov5s.yaml 中的 nc=3。更正您的标签或模型