0

I have UI program. Workflow is

  1. Click the button to install and start window service
  2. When window service is running, I click 'Add Schedule' button to add schedule to quartz
  3. If window service is started or stopped then quartz is also started or stoped

I writed code like below. But it's not working.(_scheduler is null when call AddSchedule method in ServiceManager class)

namespace CodeSamples
{
    class ServiceManager : ServiceBase
    {
        private static StdSchedulerFactory _factory;
        private static IScheduler _scheduler;
        private static readonly string _groupName = "Test";

        static ServiceManager()
        {
        }
        
        protected override async void Start(string[] args)
        {
            NameValueCollection properties = new NameValueCollection
            {
                ["quartz.jobStore.misfireThreshold"] = "5000", // 5 sec
                ["quartz.jobStore.type"] = "Quartz.Simpl.RAMJobStore"
            };

            _factory = new StdSchedulerFactory(properties);
            _scheduler = await _factory.GetScheduler();
            await _scheduler.Start();
        }

        protected override void Stop()
        {
            await _scheduler.Shutdown(false);
        }

        // Called from form class like 
        // CodeSamples.ServiceManager.AddSchedule<TestQuartz_1>("TEST");
        public static async void AddSchedule<T>(string name) where T : IJob
        {
            IJobDetail job = JobBuilder.Create<T>()
                .WithIdentity(name, _groupName)
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                        .WithIdentity($"trg_{name}", _groupName)
                        .StartNow()
                        .WithSimpleSchedule(s => s
                            .WithIntervalInSeconds(1)
                            .RepeatForever()
                            .WithMisfireHandlingInstructionIgnoreMisfires())
                        .Build();

            // _scheduler is null when  btnAddSchedule_Click event
            await _scheduler.ScheduleJob(job, trigger); 
        }
    }
}
  • It is not exactly clear what you are doing. Services are processes that run in the background, so you shouldn't see a button to click on? If you have a service and also another UI process with a button, then that is not going to work if you are using a RamStore. (It would be possible if you were using ADO.NET Job Store). If you need to use a RamStore, then you would need to find some way to pass the information to your service. – sgmoore Sep 06 '22 at 14:13
  • @sgmoore I edited my question to add workflow. Please review. – Eddie Vedder Sep 07 '22 at 01:27
  • Your UI program and service are two completely different processes and each have their own bit of memory allocated to them and neither of them have access to the other process's memory. So you need to have some way for the two processes to talk to each other. I don't think I can really help you there, expect to point you to https://stackoverflow.com/questions/2721762/what-are-windows-ipc-methods or https://learn.microsoft.com/en-gb/windows/win32/ipc/interprocess-communications?redirectedfrom=MSDN for a list of all the different methods. – sgmoore Sep 07 '22 at 08:06

0 Answers0