How do I initialize Coravel Scheduler inside a WPF Desktop App

144 Views Asked by At

I apologize early if this is a foolish or basic question I am trying to use Coravel within a C# WPF NET6 desktop application I am having problem trying to determine where you put the initialization code for Coravel such as below:

Example From Coravel Website In your .NET Core app's Startup.cs file, inside the ConfigureServices() method, add the following:

services.AddScheduler()

Then in the Configure() method, you can use the scheduler:

var provider = app.ApplicationServices;
provider.UseScheduler(scheduler =>
{
    scheduler.Schedule(
        () => Console.WriteLine("Every minute during the week.")
    )
    .EveryMinute()
    .Weekday();
});

My goal is to be able to start the scheduled tasks from several buttons withing the WPF app, I like the idea of using DI along with the Invocables but I am struggling with where I configure and insert the code above. I have been looking in the App.xaml.cs portion and overriding the OnStartup method but not sure that is the proper location.

Any help/Examples would be awesome. Thank You

This is what I have so far in the App.xaml.cs

    public partial class App : Application
    {
        //Setup the NLOG Logger
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        private const string UniqueEventName = "{396818B0-26CC-4CD7-9145-7AB6D4101196}";
        private EventWaitHandle eventWaitHandle;
        private ServiceProvider serviceProvider;

        public App()
        {

            InitializeComponent();

        }

        protected override void OnStartup(StartupEventArgs e)
        { 
            base.OnStartup(e);

            try
            {
                _logger.Info("Starting Coravel Scheduler Services...");
                IHost host = CreateHostBuilder(<"Not sure what to put here">).Build();
                host.Services.UseScheduler(scheduler =>
                {
                    // We'll fill this in later ;)
                });
                host.Run();
                _logger.Info("Completed Starting Coravel Scheduler Services...");
                return;
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex, $"There was a problem starting the Starting Coravel Scheduler Services, Error Message: {ex.Message}");
                return;
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((services) =>
                {
                    services.AddScheduler();
                });
}

0

There are 0 best solutions below