0

The problem is that on the text file i see all the results - True or Not. Even when i turned off the monitor for 5-10 seconds and turned it on on, then the text file shows True.

Not sure why it's not detecting it.

I tried this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Management;

namespace Detect_Monitor_Settings
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_AWAYMODE_REQUIRED = 0x00000040,
            ES_CONTINUOUS = 0x80000000,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_SYSTEM_REQUIRED = 0x00000001
            // Legacy flag, should not be used.
            // ES_USER_PRESENT = 0x00000004
        }

        StreamWriter w = new StreamWriter(@"e:\monitordetector.txt");

        public Form1()
        {
            InitializeComponent();
        }

        private void DetectScreenName()
        {
           if (stopdetecting == false)
            {
                var query = "select * from WmiMonitorBasicDisplayParams";
                using (var wmiSearcher = new ManagementObjectSearcher("\\root\\wmi", query))
                {
                    var results = wmiSearcher.Get();
                    foreach (ManagementObject wmiObj in results)
                    {
                        // get the "Active" property and cast to a boolean, which should 
                        // tell us if the display is active. I've interpreted this to mean "on"
                        var active = (Boolean)wmiObj["Active"];
                        w.WriteLine(active.ToString());
                    }
                }
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        bool stopdetecting = false;
        int counttimer1 = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            DetectScreenName();
            counttimer1 += 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w.Close();
            stopdetecting = true;
            timer1.Stop();
        }
    }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 2
    I think your problem is here: `// tell us if the display is active. I've interpreted this to mean "on"` Where did you get that assumption? Can you provide some documentation? – rory.ap Nov 13 '15 at 12:51
  • 1
    This is already well covered in SO Q+A, no point in repeating it. Google "wmi detect monitor turned on" to find them, the top five hits are all SO questions. Enough to know to start looking for another project. – Hans Passant Nov 13 '15 at 12:59

1 Answers1

1

As per documentation, Active

Indicates the active monitor.

That doesn't mean the monitor isn't in a power saving mode. It just tells you whether it's disabled or not - usually, this is used to detect whether you have multiple monitors or not (e.g. "is the number of active monitors higher than 1?").

Luaan
  • 62,244
  • 7
  • 97
  • 116