views:

337

answers:

1

Hey all,

I am trying to use ManagementEventWatcher in a service to keep track of when a computer goes in and out of sleep mode. I am new to .NET and C# so I am struggling quite a bit to come up with syntax to make this work.

I have found a blog post that details how he used ManagementEventWatcher to keep track of this status, but he did not post up his entire code. I am trying to go through and make a simple service that creates a .txt log file stating that the computer has been suspended/resumed but am running into problems with the namespaces and types.

Here is the code to the service.cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Management;

namespace SleepNotifierService
{
    public class WqlEventQuery : EventQuery { }

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {


            WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent");
            _watcher = new ManagementEventWatcher(query);
            _watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            _watcher.Start();
        }

        protected override void OnStop()
        {
            _watcher.Stop();
        }

        void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            try
            {
                int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
                switch (eventType)
                {
                    case 4:
                        Sleep();
                        break;
                    case 7:
                        Resume();
                        break;
                }
            }
            catch (Exception ex)
            {
                //Log(ex.Message);
            }
        }

        public void Sleep()
        {
        }

        public void Resume()
        {
        }

    }
}

Again, this is the first time that I am programming with .NET and C# so I apologize for my ignorance.

I am getting namespace errors such as:

The type or namespace name 'ManagementEventWatcher' could not be found (are you missing a using directive or an assembly reference?)

Thanks,

Tomek

+1  A: 

You need the System.Management namespace, which is included in the code sample provided by you. I believe you need to reference the System.Management library in your project settings. Follow the following steps to do this( I am assuming you are suing Visual Studio):

Go to the Solution Explorer, and expand your project, right click on the References folder/option and select Add References from the context menu. Now select the .Net tab and select the System.Management from the list and click OK.

thanks for the advice, now I am getting an error that WqlEventQuery "does not contain a constructor that takes '1' arguments". I have been searching online for some solution and it seems like people have been using WqlEventQuery by passing one argument. I am going to have to do some more research.
Tomek
You are probably getting that error because of the following line of code:public class WqlEventQuery : EventQuery { }that line basically redefines the WqlEventQuery class. And since you do not have any constructors defined for this class, the default constructor will be used.