E
E
estry2020-08-09 17:22:24
C++ / C#
estry, 2020-08-09 17:22:24

Are there solutions for scheduling in C#?

For example, there is a class MyClass and a method Void.
I need to run the Void method 10 times a day. 8 times in the time interval from 6 am to 11 pm and 2 times from 11 pm to 6 am. In this case, the launch intervals should be random.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-08-09
@estry

Let it be so, repeat in a day

// 8 раз в промежутке времени с 6 утра до 23 вечера и 2 раза с 23 вечера до 6 утра.
            var now = DateTime.Now;
            var startPeriod = (23 - 6) * 60;
            var startList = new List<int>();
            while (startList.Count < 8)
            {
                var val = RandomNumberGenerator.GetInt32(0, startPeriod);
                if (!startList.Contains(val))
                    startList.Add(val);
            }

            var runList = startList.OrderBy(x => x).Select(item => new DateTime(now.Year, now.Month, now.Day).AddMinutes(6 * 60 + item)).ToList();

            startList.Clear();
            var endPeriod = (1 + 6) * 60;
            while (startList.Count < 2)
            {
                var val = RandomNumberGenerator.GetInt32(0, endPeriod);
                if (!startList.Contains(val))
                    startList.Add(val);
            }

            runList.AddRange(startList.OrderBy(x => x).Select(item => new DateTime(now.Year, now.Month, now.Day).AddMinutes(23 * 60 + item)));
            ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question