Reputation: 1961
There is a program X that activates itself on my computer (and i can't get rid of), I built a program in c# that checks to see which tasks are currently activated (like task manager in windows), when it sees that X is activated it shuts it off,
now, the problem is that if i do this, i am using polling with a timer control, and that causes a lot of CPU usage, and i was wondering if there's another way to do this. This problem has been nagging me in other programs i made too.
p.s: program X is mobsync, which is a service in windows vista responsible for mobile devices synchronization, it also opens windows media player in the background and takes a lot of CPU. i have searched for another solution online, but only found another program that somebody else built which i don't trust.
Upvotes: 1
Views: 938
Reputation: 45101
As Arnaud had already written in its comment you should really go into the Services panel and simply set the start mode from Automatic
to Disabled
(or Manual
if you like).
Then the service won't be started anymore and you don't need any kind of watcher.
Also there is no other model than polling available for this kind of task. The easiest way to prove is that even ProcessExplorer from Mark uses a polling algorithm and i think he has a very deep understanding of the system and how to get some information efficently out of it.
Upvotes: 1
Reputation: 8452
As said in comment, the best way is to disable it in the control panel, if you cant or want to do it programmatically, it's easy to do it whitout consuming a lot of CPU:
using System.ServiceProcess;
class Program
{
private const int _delay = 1000;
private const int _period = 1000;
private const string _serviceName = "mobsync";
public static void Main(string[] args)
{
var sc = ServiceController.GetServices().FirstOrDefault(__s => __s.DisplayName == _serviceName);
// If service found, launch the timer
if (sc != null)
{
System.Threading.Timer timer = new System.Threading.Timer((o) =>
{
var service = (ServiceController)o;
try
{
if (sc.Status == ServiceControllerStatus.Running)
sc.Stop();
}
catch
{
Console.WriteLine("Error when stopping service");
}
}, sc, _delay, _period);
}
else
{
Console.WriteLine("Unknown service : " + _serviceName);
}
Console.ReadLine();
}
}
Upvotes: 2