what is the maximum time windows service wait to process stop request and how to request for additional time

I wrote the following code to achieve it. protected override void OnStop() { int timeout = 10000; var task = Task.Factory.StartNew(() => MyTask()); while (!task.Wait(timeout)) { RequestAdditionalTime(timeout); } } The above code starts a Task in Parallel to the main thread (Task start running immediately), next line is to check if task is completed or … Read more

Can I have multiple services hosted in a single windows executable

I created a 3 service project (below) which uses a project installer for each service. I then added an installer project which installs the services into service manager. Here was my workflow: Create 3 services in a solution in Visual Studio 2008. Naming each service as Service1, Service2 and, Service3. (Being sure to change the … Read more

C#: GUI to display realtime messages from Windows Service

What you can do is have the windows service have way of registering for an event (you can do this through using Windows Communication Foundation). When your error comes up, it fires that event, and your winforms app will be notified. It’s called a duplex contract: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/0eb69998-0388-4731-913e-fb205528d374/ http://msdn.microsoft.com/en-us/library/ms731184.aspx Actually the really cool thing is that … Read more

Hosting WCF service inside a Windows Forms application

This code should be enough to get you started: Form1.cs namespace TestWinform { public partial class Form1 : Form { private ServiceHost Host; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Host = new ServiceHost(typeof(MyWcfService)); Host.Open(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Host.Close(); } } } App.config <?xml version=”1.0″ … Read more

Schedule machine to wake up

You can use waitable timers to wake from a suspend or hibernate state. From what I can find, it is not possible to programmatically wake from normal shut down mode (soft off/S5), in that case, you need to specify a WakeOnRTC alarm in BIOS. To use waitable timers from C#, you need pInvoke. The import … Read more

Check status of services that run in a remote computer using C#

If you use WMI, you can set the credentials in ‘ConnectionOptions’. ConnectionOptions op = new ConnectionOptions(); op.Username = “Domain\\Domainuser”; op.Password = “password”; ManagementScope scope = new ManagementScope(@”\\Servername.Domain\root\cimv2″, op); scope.Connect(); ManagementPath path = new ManagementPath(“Win32_Service”); ManagementClass services; services = new ManagementClass(scope, path, null); foreach (ManagementObject service in services.GetInstances()) { if (service.GetPropertyValue(“State”).ToString().ToLower().Equals(“running”)) { // Do something } … Read more