Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 577835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:11:46+00:00 2026-05-13T14:11:46+00:00

I am trying to create a simple service in C# using VS2008 that creates

  • 0

I am trying to create a simple service in C# using VS2008 that creates a text file when the computer goes into sleep mode. My current code throws out the following error:

‘SleepNotifierService.WqlEventQuery’ does not contain a constructor that takes ‘1’ arguments

Now I looked in the Object browser, and it looks like it does take in one argument. This is what the browser had to say:

public WqlEventQuery(string queryOrEventClassName)

Here is my code:

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;
using System.IO;

namespace SleepNotifierService
{
    public class WqlEventQuery : EventQuery { }

    public partial class Service1 : ServiceBase
    {
        ManagementEventWatcher _watcher;

        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()
        {
            StreamWriter SW;
            SW = File.CreateText("c:\\MyTextFile.txt");
            SW.WriteLine("Sleep mode initiated");
            SW.Close();
        }

        public void Resume()
        {
        }

    }
}

Am I interpreting that object browser wrong? I’m new to creating services and C#/.NET in general so it might be something trivial.

Appreciate any help,

Tomek

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:11:47+00:00Added an answer on May 13, 2026 at 2:11 pm

    You’re using wrong WqlEventQuery. There’s one defined in System.Management and it indeed has a one-argument constructor, but there’s also your custom WqlEventQuery class.

    If you want to use .NET BCL’s class, you’ll have to fully qualify it:

    var query = new System.Management.WqlEventQuery("Win32_PowerManagementEvent");
    

    or even prefix it with global keyword:

    var query = new global::System.Management.WqlEventQuery("Win32_PowerManagementEvent");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.