Archive for category Services

Why is svchost.exe consuming all my CPU / disk?

If you’ve ever opened your task manager to see what is running on your machine you’ve likely wondered why svchost is running, why it’s running more than once, and why some take much more CPU than others.

Well the quick answer to your question is svchost is really not the process being run. svchost is what windows uses to run what are known as “Windows Services”. To view the Windows Services that are installed on your computer go here:
Control Panel->Administrative Tools->Services

Every service listed in Windows Services will spawn a svchost process when it runs. So in order to know why a particular svchost process is eating your computer you’ll need to track down which Windows Service it is. Thankfully there is a command line tool that makes this pretty easy. In Task Manager take note of the Process Id (PID) of the svchost in question. Then run this from the command line:
tasklist /fi “pid eq XXX” /svc
Where XXX is equal to the PID you took note of from Task Manger.

If you remembered the /svc switch you will see all the services your process depends on. If you’re having issues with non responsiveness it’s these service(s) that need to be dealt with.

Better yet, if you have Windows Vista or newer, in Task Manager right click the process in question and select “Go to service(s)”. This will show you a list of all the services running under that instance of svchost.

This site has pretty good detail on this topic: http://www.howtogeek.com/howto/windows-vista/what-is-svchostexe-and-why-is-it-running/

Happy process hunting!

Leave a comment

Creating a new Windows Service .Net

Microsoft has made it a bit painful to develop a windows service IMO.  If you have the right version of Visual Studio it does make it easier, apparently the ‘regular’ version doesn’t come with the Windows Service template, and if you use VS Express you’re out of luck.

However, regardless of Visual Studio version you ARE able to create Windows Services, it’s just with some versions you have to do the entire thing by hand while with others it generates you a template.

In case you’re one of the many that don’t have the template here is the bare minimum for a windows service:

using System.Threading;

class Program : System.ServiceProcess.ServiceBase
{
private Timer serviceTimer;
TimerCallback timerDelegate;
private int _interval = 500000;

public Program(){
{
this.ServiceName = “My name”;
}
protected override void OnStart(string[] args)
{
//Code here to repeat the call, DoWork() will be our main entry point:
timerDelegate = new TimerCallback(DoWork);
// create timer and attach our method delegate to it
serviceTimer = new Timer(timerDelegate, null, 10000, _interval);
}
}
protected override void OnStop(){
//Terminate code here…
}
private void DoWork(object state){
//Main program code here:
//after Main program run again:
timerDelegate = new TimerCallback(DoWork);
// create timer and attach our method delegate to it
serviceTimer = new Timer(timerDelegate, null, 10000, _interval);
}

Now, that’s great and all but there are still a few gotcha’s.  For example, you can’t debug / step through this code.  In order to do that you’ll need an entry point that’s different than what a windows service needs.  In my project I used this:

static void Main(string[] args)
{

Program p = new Program();
p.OnStart(args);

while (true)
{
System.Threading.Thread.Sleep(5000);
}
}

Note this code only gets called while you’re debugging, so the infinite loop isn’t really an issue, and is actually similar to how a service works (never exits unless forced to somehow).

So you’ve written your service but don’t know how to actually create the service?  Yeah, there’s no right-click->add new in the admin interface.  In order to create a new service you’ll need to run sc.exe.  This command line is a good example:

sc create “ATestService” binpath= “c:\TheFullPath\AndExeName.exe”
NOTE: the space after the = sign is required!

Here’s some more info on sc.exe:
Using SC.EXE to Develop Windows NT Services

REMEMBER: this service is defaulted to run as system.  You will probably want it to run as someone in particular (certainly someone who has the privilege necessary to run anything using the current user for authentication.  eg: You may not need to worry about databases depending on how you’ve set them up, but writing to the file system you’ll need an appropriate user for).  To configure this service to run as someone else just open the Services admin panel->r-click on the service->Properties->Log On(tab)->This account: and now choose the appropriate id/pwd.

Leave a comment