How to detect whether monitor turned off/on?

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();
        }
    }
}
c#
.net
winforms
asked on Stack Overflow Nov 13, 2015 by Sharondohp Sharonas • edited Nov 13, 2015 by StepUp

1 Answer

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?").

answered on Stack Overflow Nov 13, 2015 by Luaan

User contributions licensed under CC BY-SA 3.0