Thursday, May 14, 2009

Use ServiceController to Start or Stop a Service

Reference the System.ServiceProcess.dll first, and use the namespace System.ServiceProcess:

We write the code sample as the following:

using System;
using System.ServiceProcess;

class Program
{
static void Main(string[] args)
{
ServiceController sc = new ServiceController("Server");
string svStatus = sc.Status.ToString();
if (svStatus == "Stopped")
{ sc.Start(); }
if (svStatus == "Running")
{ sc.Stop(); }
}
}

Tuesday, May 5, 2009

Use ThreadPool to Queue Work Items

Example:

using System.Threading;

static void Main(string[] args)
{
WaitCallback callback=new WaitCallback(SomeMethod);
ThreadPool.QueueUserWorkItem(callback, "Hi");
ThreadPool.QueueUserWorkItem(callback, "Good Morning");
ThreadPool.QueueUserWorkItem(callback, "Good Bye");
}

static void SomeMethod(object state)
{
string greetingText=(string)state;
Console.Write(geetingText);
}

The step here is Use WaitCallBack delegate to call SomeMethod() and use ThreadPool static method to queue up serverl calls, parameter/object can be passed.