mirror of
https://github.com/SlothDpal/Relaunch-Process.git
synced 2026-02-22 17:27:38 +03:00
Добавьте файлы проекта.
This commit is contained in:
228
Form1.cs
Normal file
228
Form1.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using RelaunchProcess.Properties;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace Process_Auto_Relaunch
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
private delegate void UpdateLogDelegate(string text);
|
||||
private UpdateLogDelegate updateLogDelegate = null;
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.updateLogDelegate = new UpdateLogDelegate(this.UpdateStatus);
|
||||
myBackgroundWorker.WorkerSupportsCancellation = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ñîáûòèå çàïóñêà ôîðìû
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
LoadOldState();
|
||||
|
||||
|
||||
CheckProgramState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Âîññòàíîâëåíèå íàñòðîåê
|
||||
/// </summary>
|
||||
private void LoadOldState()
|
||||
{
|
||||
if (Settings.Default.saveOldState)
|
||||
{
|
||||
radioButtonEnableWathing.Checked = Settings.Default.enableWatching;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void radioButtonDisableWathing_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
CheckProgramState();
|
||||
|
||||
if (!radioButtonDisableWathing.Checked)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (myBackgroundWorker.WorkerSupportsCancellation && myBackgroundWorker.IsBusy)
|
||||
{
|
||||
myBackgroundWorker.CancelAsync();
|
||||
UpdateStatus("Îòìåíÿåì...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void radioButtonEnableWathing_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (!radioButtonEnableWathing.Checked)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bool error = false;
|
||||
|
||||
if (String.IsNullOrEmpty(textBoxProcessName.Text))
|
||||
{
|
||||
error = true;
|
||||
MessageBox.Show("Èìÿ ïðîöåññà íå ìîæåò áûòü ïóñòûì!" +
|
||||
"\nÓêàæèòå èìÿ ïðîöåññà", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(Settings.Default.startProgramPath))
|
||||
{
|
||||
error = true;
|
||||
MessageBox.Show("Ïðîãðàììà äëÿ çàïóñêà íå óêàçàíà." +
|
||||
"\nÓêàæèòå ïðîãðàììó äëÿ çàïóñêà", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
if (error)
|
||||
{
|
||||
radioButtonEnableWathing.Checked = false;
|
||||
radioButtonDisableWathing.Checked = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!myBackgroundWorker.IsBusy)
|
||||
{
|
||||
myBackgroundWorker.RunWorkerAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStatus(string text)
|
||||
{
|
||||
labelStatus.Text = text;
|
||||
}
|
||||
|
||||
private void Status(string text)
|
||||
{
|
||||
Invoke(updateLogDelegate, new[] { text });
|
||||
}
|
||||
|
||||
private void CheckProgramState()
|
||||
{
|
||||
bool watching = radioButtonEnableWathing.Checked;
|
||||
Debug.WriteLine($"Íàáëþäåíèå: {watching}");
|
||||
|
||||
groupBoxProcessName.Enabled = !watching;
|
||||
groupBoxProgramStart.Enabled = !watching;
|
||||
groupBoxActions.Enabled = !watching;
|
||||
|
||||
Settings.Default.enableWatching = watching;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Âûáîð ôàéëà äëÿ çàïóñêà
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonSetProgramStart_Click(object sender, EventArgs e)
|
||||
{
|
||||
OpenFileDialog openFile = new OpenFileDialog();
|
||||
openFile.Title = "Óêàæèòå ïðîãðàììó çàïóñêà";
|
||||
|
||||
if (openFile.ShowDialog() == DialogResult.Cancel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//labelProgramStartPath.Text = openFile.FileName;
|
||||
Settings.Default.startProgramPath = openFile.FileName;
|
||||
Settings.Default.Save();
|
||||
|
||||
openFile.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ñîáûòèå ïåðåä çàêðûòèåì ôîðìû
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
Settings.Default.Save();
|
||||
}
|
||||
|
||||
private bool ProcessByNameIsRuning(string name)
|
||||
{
|
||||
return Process.GetProcessesByName(name).Length > 0;
|
||||
}
|
||||
|
||||
private void ProcessStart(string path, string args)
|
||||
{
|
||||
if (checkBoxCheckProcess.Checked)
|
||||
{
|
||||
if (ProcessByNameIsRuning(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Process.Start(path, args);
|
||||
}
|
||||
|
||||
private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
|
||||
{
|
||||
BackgroundWorker worker = sender as BackgroundWorker;
|
||||
int i = (int)numericUpDown1.Value;
|
||||
|
||||
while (!worker.CancellationPending)
|
||||
{
|
||||
if (ProcessByNameIsRuning(textBoxProcessName.Text))
|
||||
{
|
||||
Status($"Ïðîöåññ {textBoxProcessName.Text} óæå çàïóùåí");
|
||||
i = (int)numericUpDown1.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (radioButtonRestartTimer.Checked)
|
||||
{
|
||||
i--;
|
||||
Status($"Ïðîöåññ {textBoxProcessName.Text} íå íàéäåí. Çàïóñê ÷åðåç {i}");
|
||||
}
|
||||
|
||||
if (i <= 0 || radioButtonRestartNow.Checked)
|
||||
{
|
||||
Status("Çàïóñêàåì...");
|
||||
ProcessStart(Settings.Default.startProgramPath, textBoxArguments.Text);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
if (worker.CancellationPending)
|
||||
{
|
||||
e.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
||||
{
|
||||
if (e.Cancelled)
|
||||
{
|
||||
Status("Íàáëþäåíèå îòìåíåíî.");
|
||||
}
|
||||
else if (e.Error != null)
|
||||
{
|
||||
MessageBox.Show("Error: " + e.Error.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Stop);
|
||||
Status("Ïðîèçîøëà îøèáêà. Íàáëþäåíèå îñòàíîâëåíî!");
|
||||
radioButtonDisableWathing.Checked = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status("Íàáëþäåíèå îñòàíîâëåíî.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user