Zdravim, Potreboval bych poradit, potrebuji vytvorit sluzbu ktera kdyz zjisti spustenej program v pameti tak ho ukonci. Bohuzel se mi to nedari.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace SimpleWindowsService
{
public partial class SimpleService : ServiceBase
{
private BackgroundWorker BW;
public SimpleService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("SimpleSource"))
System.Diagnostics.EventLog.CreateEventSource("SimpleSource", "SimpleLog");
eventLogSimple.Source = "SimpleSource";
eventLogSimple.Log = "SimpleLog";
BW = new BackgroundWorker();
BW.WorkerSupportsCancellation = true;
BW.DoWork += new DoWorkEventHandler(BW_DoWork);
BW.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BW_RunWorkerCompleted);
}
protected override void OnStart(string[] args)
{
BW.RunWorkerAsync(args);
eventLogSimple.WriteEntry("Simple Service started!");
}
protected override void OnStop()
{
eventLogSimple.WriteEntry("Simple Service stopped!");
}
void BW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
BW.CancelAsync();
while (BW.IsBusy) Thread.Sleep(1000);
eventLogSimple.WriteEntry(ServiceName + " completed");
}
void BW_DoWork(object sender, DoWorkEventArgs e)
{
while (!BW.CancellationPending)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process process in processes)
{
if (process.ProcessName == "taskmgr.exe")
{
process.Kill();
eventLogSimple.WriteEntry("Proces " + process.ProcessNam + "byl ukončen!");
}
}
Trace.WriteLine("ComService: " + new Random().Next().ToString());
Thread.Sleep(1000);
}
}
}
}
|