using Microsoft.Win32; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ExternalProgramBatcher { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private string _path; private int _interval; private System.Timers.Timer _timer; public MainWindow() { InitializeComponent(); InitInstance(); } private void InitInstance() { _timer = new System.Timers.Timer(); _timer.Elapsed += _timer_Elapsed; } private void btnPath_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() != true) return; tbPath.Text = dlg.FileName; } private void btnStart_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(tbPath.Text)) return; if (string.IsNullOrEmpty(tbInterval.Text)) return; try { _path = tbPath.Text; _timer.Interval = int.Parse(tbInterval.Text); _timer.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private bool _isStartAction = true; private void _timer_Elapsed(object? sender, ElapsedEventArgs e) { if (_isStartAction) { Process.Start(_path); _isStartAction = !_isStartAction; } else { string processName = System.IO.Path.GetFileNameWithoutExtension(_path); foreach (Process process in Process.GetProcesses()) { if (process.ProcessName.StartsWith(processName)) process.Kill(); } _isStartAction = !_isStartAction; } } } }