Как выполнить код если процесс найден (if) или не найден (else) - C#
Формулировка задачи:
вот пример!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace закрыть_по_таймеру
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 2000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
System.Diagnostics.Process[] processes;
processes = Process.GetProcessesByName("chrome");
foreach (System.Diagnostics.Process instance in processes)
//if (???????????)
{
listBox1.Items.Add(instance.ProcessName);
//timer1.Stop();
}
// else
// {
// listBox1.Items.Add("нет процесса"); как выполнить код если процесс не найден ?
// }
}
private void GetProcess(string p)
{
throw new NotImplementedException();
}
}
}Решение задачи: «Как выполнить код если процесс найден (if) или не найден (else)»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace Закрыть_по_таймеру2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
string processName = "PlantsVsZombies";
var arrayProcesses = Process.GetProcessesByName(processName);
if (arrayProcesses == null || arrayProcesses.Length < 1)
{
//MessageBox.Show("Процесс не найден");
listBox1.Items.Add("Процесс не найден");
}
else
{
Process currentProcess = arrayProcesses[0];
//MessageBox.Show(currentProcess.MainModule.ModuleName.ToString());
listBox1.Items.Add("Процесс найден");
}
}
}
}