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.Diagnostics;
namespace ProcessKiller
{
public partial class Form1 : Form
{
bool isRun = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (isRun)
{
string pName = textBox1.Text;
Process[] pList = Process.GetProcessesByName(pName);
StringBuilder sb = new StringBuilder();
label1.Text = "";
foreach (Process p in pList)
{
DateTime startTime = p.StartTime;
TimeSpan runTime = DateTime.Now - startTime;
int limitAddTime = int.Parse(comboBox1.Text);
DateTime limitTime = p.StartTime.AddMinutes(limitAddTime);
sb.Append("[ID:"+ p.Id +"] [プロセス名:" + p.ProcessName + "] [稼働時間:" +
String.Format("{0:00}",runTime.Hours) + ":" + String.Format("{0:00}",runTime.Minutes) + ":" + String.Format("{0:00}",runTime.Seconds) + "] [停止予定時刻:" + limitTime + "]" + System.Environment.NewLine);
var limitTimeOfDay = limitTime.TimeOfDay;
if ((limitTimeOfDay - startTime.TimeOfDay) < runTime)
{
p.Kill();
label4.Text = p.ProcessName + "(ID:" + p.Id + ")をkillしました";
}
}
label1.Text = sb.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
string pName = textBox1.Text;
if (String.IsNullOrEmpty(pName))
{
MessageBox.Show("プロセス名を入力してください");
return;
}
timer1.Enabled = true;
isRun = true;
label4.Text = "監視開始します";
}
private void button2_Click(object sender, EventArgs e)
{
isRun = false;
label1.Text = "";
label4.Text = "";
}
}
}