B
B
beduin012020-07-29 16:42:37
Windows
beduin01, 2020-07-29 16:42:37

How to restart application after n minutes?

You need to restart the application every 2 minutes. Are there ready-made solutions? If not, how do you write? Accept as an argument the name of the desired software with arguments to run it? And then set a timer?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2020-07-29
@beduin01

Threw something approximate in C #

The code

try
            {
                Console.WriteLine("Введите путь к приложению:");
                string path = Console.ReadLine().Trim();
                if (!File.Exists(path)){
                    Console.WriteLine("Файл не найден");
                    Console.ReadKey();
                    return;
                }
                else
                {
                    Console.WriteLine("Введите аргументы запуска(не обязательно):");
                    string startupArgs= Console.ReadLine().Trim();

                    Console.WriteLine("Введите время работы таймера например 10sec или 5min или 3hour или 3hour 5min 10sec");
                    string time = Console.ReadLine().Trim();
                    int ms = 0;
                    var sec = Regex.Match(time, @"(\d+)\s*sec");
                    if (sec.Success)
                        ms += Int32.Parse(sec.Groups[1].Value) * 1000;
                    var min= Regex.Match(time, @"(\d+)\s*min");
                    if (min.Success)
                        ms += Int32.Parse(min.Groups[1].Value) * 60*1000;
                    var hour = Regex.Match(time, @"(\d+)\s*hours?");
                    if (hour.Success)
                        ms += Int32.Parse(hour.Groups[1].Value) *60 * 60 * 1000;
                    if (ms < 5000){
                        Console.WriteLine("Таймер минимум на 5сек");
                        Console.ReadKey();
                        return;
                    }
                    Console.WriteLine("Закрывать ли другие копии этого процесса? y/n");
                    bool isCloseOthers = false;
                    while (true)
                    {
                        var key = Console.ReadKey(true).Key;
                        if ( key == ConsoleKey.Y)
                        {
                            isCloseOthers = true;
                            break;
                        }
                        else if (key == ConsoleKey.N)
                        {
                            isCloseOthers = false;
                            break;
                        }
                    }

                    Console.WriteLine("Перезагружать таймер автоматически каждый раз? y/n");
                    bool isAutoReset = false;
                    while (true){
                        var key = Console.ReadKey(true).Key;
                        if (key== ConsoleKey.Y)
                        {
                            isAutoReset = true;
                            break;
                        }
                        else if (key == ConsoleKey.N)
                        {
                            isAutoReset = false;
                            break;
                        }
                    }

                    Timer t = new Timer(ms);
                    t.AutoReset = isAutoReset;
                    t.Elapsed += (sender, eventArgs) =>{
                        Console.WriteLine("Перезапуск! Для отключения таймера нажмите X");
                        var p = Process.Start(path, startupArgs);
                        if (isCloseOthers){
                            var duplicates = Process.GetProcessesByName(p.ProcessName).Where(process => process.Id != p.Id)
                                .ToList();
                            foreach (var duplicate in duplicates)
                                duplicate.Kill();
                        }
                    };
                    Console.WriteLine("Таймер запущен(" + TimeSpan.FromMilliseconds(t.Interval).ToString(@"hh\:mm\:ss") + ")! Для отключения таймера нажмите X");
                    t.Start();

                    while (Console.ReadKey(true).Key != ConsoleKey.X) { }
                    t.Stop();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }


Build

D
Dimonchik, 2020-07-29
@dimonchik2013

cron/scheduler linux/win

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question