c# - Use NativeWindow to disable screensaver -


i want disable screensaver , monitor power off. @ stage there's no windows form, youse. wan't use nativewindow.

here's code

sealed class observerwindow : nativewindow, idisposable {     internal observerwindow()     {         this.createhandle(new createparams()         {             parent= intptr.zero         });      }      public void dispose()     {         destroyhandle();     }      protected override void wndproc(ref message msg)     {         if (msg.msg == wm_syscommand &&             ((((long)msg.wparam & 0xfff0) == sc_screensave) ||             ((long)msg.wparam & 0xfff0) == sc_monitorpower))         {             msg.msg = 0;             msg.hwnd = intptr.zero;         }         base.wndproc(ref msg);     } } 

the problem is, wndproc not called wm_syscommand. actualy wndproc called 4 times. @ last call there's msg.msg == wm_create.

i think i'm missing create parameter. have advise?

regards michael

update

i running code in non sta thread. window did not reveive messages exept initial ones. i'm receiving wm_syscommand messages. when screensaver activated, there's no message.

i tried overwrite form's wndproc same result. used work in windows xp. there change in windows 7?

os: windows 7 64bit.

solution

as comment in question states, foreground window can cancel screensaver. above code can't work. nativewindow great receiving messages, not canceling screensaver. latter recommend answer question.

the proper way telling windows thread needs have display active. commonly used video players. p/invoke setthreadexecutionstate() api function, pass es_display_required. , es_system_required keep machine shutting down automatically. visit pinvoke.net required declarations.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -