3

I have a simple task I was trying to get to repeat using FluentScheduler, but I can't seem to get it to run properly. I am entirely new to "Jobs" and "Actions", so I am sure it is something stupid I am not accounting for.

I am modifying my original question as the supplied solutions were working for a console app, but not a Windows Forms app. I have tried moving the line Application.Run(ThisForm); to be before and after the JobManager and the Task, but it never updates the TextBox with the string.

  public class Program
    {

        public class MyRegistry : Registry
        {
            public MyRegistry()
            {
                Action someMethod = new Action(() =>
                {
                    Form1 ThisForm = new Form1();
                    ThisForm.Text ="HELLO";
                });


                this.Schedule(someMethod).ToRunNow().AndEvery(2).Seconds();
            }

        }


        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(ThisForm);

            JobManager.Initialize(new MyRegistry());
            var t = Task.Run(async delegate
            {
                await Task.Delay(-1);
            });

            t.Wait();

        }
  }
Alan
  • 2,046
  • 2
  • 20
  • 43
  • 1
    Your console application will automatically finish execution once the final line has been ran, therefore when your JobManager has initialized, it will run and then stop execution. You will need to make your application wait – Sasha Jan 05 '18 at 13:52
  • I want this to be a background task that happens all the time, what is the least costly way (CPU and memory) to keep it active? – Alan Jan 05 '18 at 13:54
  • 1
    You could make use of `async` functionality and have an `await Task.Delay(-1)`, which would make it run until closed down – Sasha Jan 05 '18 at 13:58
  • Will this be in a console application, or in a windows service? – Sasha Jan 05 '18 at 13:58
  • Possible duplicate of [How to use FluentScheduler library to schedule tasks in C#?](https://stackoverflow.com/questions/42978573/how-to-use-fluentscheduler-library-to-schedule-tasks-in-c) – A Friend Jan 05 '18 at 13:59
  • Question is slightly different, but answers should cover – A Friend Jan 05 '18 at 13:59
  • I updated my question. This is my first C# project and so just fleshing out some of the proof of concept things. It will be a Windows service with a taskbar icon. It needs to do an http request every few minutes to retrieve JSON and then do something with it. – Alan Jan 05 '18 at 16:02

0 Answers0