Reference to Main MainViewModel C# WPF from another class Quartz.Net (CS0120)
-
There is a problem with reference to the non-statistical field.
The annex contains the main class
public class MainViewModel : ObservableObject
in which a number of methods and tasks are carried out e.g.
public async void Method2()
There's also a timer cron inside the class using Quarz.Net Library. and a separate class of work performed. Timer
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") .WithCronSchedule("0 0/1 * * * ?") .ForJob("job1", "group1") .StartNow() .Build(); await scheduler.ScheduleJob(job, trigger); } catch (SchedulerException se) { } }
starting and doing okay. I've been having trouble doing my job.
public class MyJob : IJob
{// public string MyCommand { private get; set; } public async Task Execute(IJobExecutionContext context) { Method2(); } }
The reference not to Static method is a mistake.
Ошибка CS0120 Для нестатического поля, метода или свойства "MainViewModel.Method2()" требуется ссылка на объект.
Examples of solutions https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/compiler-messages/cs0120
It says we need a new copy of the class.
public class MyJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var mc = new MainViewModel();
mc.Method2();
}
}
but then all the method ' s actions will be taken inside a new copy and not updated inside the old one.
Also, as an option, it is written to add in Method2 static, but inside MainViewModel many async methods that cause the same problem when changing the part of the code on static.
Question: As from the MyJob class of work, call acync method MainViewModel, which is launched from WPF XAML. As to the reference to this very model at the launch of Method2();
Or any other solution to the problem with reference to the object.
-
Thank you so much. https://ru.stackoverflow.com/users/220553/evgeniyz for helping. Comments on Russian indicated what was added to the code.
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") .Build(); job.JobDataMap.Put("vm", this); //Добавление названия MainViewModel в JobDataMap // Define the Trigger ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .WithCronSchedule("0 0/1 * * * ?") .ForJob("job1", "group1") .StartNow() .Build(); await scheduler.ScheduleJob(job, trigger); } catch (SchedulerException se) { } } public class MyJob : IJob { // public string MyCommand { private get; set; } public async Task Execute(IJobExecutionContext context) { var vm = (MainViewModel)context.JobDetail.JobDataMap["vm"]; //Передача имени/ссылки MainViewModel в класс MyJob из JobDataMap vm.Method(); //Запускаемый метод/таск из MainViewModel } }