Accessing shared network drives prevents standby mode

J

JT

Hi,

I've realized that my good fortune of not having my computer go into
standby mode was the result of my application accessing a file on a
shared network drive. Not going into standby is a very good thing for
my application, but I don't want users to be required to share a drive
and file just to accomplish this.

I've read in old posts in various places stating that by importing
kernel32.dll I could access a function called
SetThreadExecutionState(...) with a value of ES_SYSTEM_REQUIRED,
potentially ORed with ES_CONTINUOUS (or the equivalent DWORD values),
would accomplish what I need. I'm guessing this is not managed code
and would have to be marshalled in and used as unsafe code (please
forgive me for not knowing anything about marshalling or unmanaged
code).

Is there a managed code way to accomplish this? Does anyone have any
suggestions?

Thanks,

JT
 
N

Nicholas Paldino [.NET/C# MVP]

JT,

You ^might^ be able to do this through the WMI provider for windows,
which means you would access the System.Management namespace, but to be
honest, doing it through the P/Invoke layer is ^not^ that big of a deal.
The declaration for the function is at:

http://www.pinvoke.net/default.aspx/kernel32/SetThreadExecutionState.html

Also, you don't need unsafe code to call this. But a call through the
P/Invoke layer is required. In this case though, it's trivial. Some
sections of the .NET framework are completely reliant on the P/Invoke layer
(pretty much everything in the System.Windows.Forms namespace).
 
J

JT

Thanks Nicholas,

Before my first post I did a quick scan through WMI, and quickly got
lost, never having looked at it before, but I didn't see anything
relating to what I need. I will go look at your link and investigate
P/Invoke. I've never dealt with that either.

JT

JT,

    You ^might^ be able to do this through the WMI provider for windows,
which means you would access the System.Management namespace, but to be
honest, doing it through the P/Invoke layer is ^not^ that big of a deal.
The declaration for the function is at:

http://www.pinvoke.net/default.aspx/kernel32/SetThreadExecutionState....

    Also, you don't need unsafe code to call this.  But a call through the
P/Invoke layer is required.  In this case though, it's trivial.  Some
sections of the .NET framework are completely reliant on the P/Invoke layer
(pretty much everything in the System.Windows.Forms namespace).

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I've realized that my good fortune of not having my computer go into
standby mode was the result of my application accessing a file on a
shared network drive.  Not going into standby is a very good thing for
my application, but I don't want users to be required to share a drive
and file just to accomplish this.
I've read in old posts in various places stating that by importing
kernel32.dll I could access a function called
SetThreadExecutionState(...) with a value of ES_SYSTEM_REQUIRED,
potentially ORed with ES_CONTINUOUS (or the equivalent DWORD values),
would accomplish what I need.  I'm guessing this is not managed code
and would have to be marshalled in and used as unsafe code (please
forgive me for not knowing anything about marshalling or unmanaged
code).
Is there a managed code way to accomplish this?  Does anyone have any
suggestions?

JT- Hide quoted text -

- Show quoted text -
 
J

JT

Nicholas,

I just did my first P/Invoke and it works like a champ (on preliminary
evaluation)! Are there any caveats about P/Invokes?

Thanks,

JT
 
N

Nicholas Paldino [.NET/C# MVP]

JT,

Well, as with anything, there are caveats. P/Invoke can be a little
overwhelming, depending on the kind of unmanaged code that you have to make
calls to (in some cases you have to worry about if the function is
allocating memory, manual marshalling, whether or not the data you marshal
is used past the call to the function, etc, etc). However, I wouldn't say
there are really any "unknowns" when it comes to the P/Invoke layer. It is
very well defined and predictable. It's the code that you are calling that
isn't usually.

With the WMI classes and the System.Management namespace, there isn't
going to be anything specific to this issue. The System.Management
namespace is used to write queries against WMI. It provides a general
object model to access WMI classes, which there are providers for.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,

I just did my first P/Invoke and it works like a champ (on preliminary
evaluation)! Are there any caveats about P/Invokes?

Thanks,

JT
 
W

Willy Denoyette [MVP]

Keep in mind that calling this API doesn't prevent a *user* to put the
system in Sleep state.

Willy.


Nicholas,

I just did my first P/Invoke and it works like a champ (on preliminary
evaluation)! Are there any caveats about P/Invokes?

Thanks,

JT
 
J

JT

Hey Willy,

I've given the user a preference setting to let him decide how he
wants the application to behave. I figure if he's set it so that it
will not go into full standby, then if he purposely places it in that
mode, he has a reason for it. The application doesn't crash if it's
in standby.

So what would a user do to cause standby mode when the application
setting has indicated otherwise?

JT

Keep in mind that calling this API doesn't prevent a *user* to put the
system in Sleep state.

Willy.


Nicholas,

I just did my first P/Invoke and it works like a champ (on preliminary
evaluation)!  Are there any caveats about P/Invokes?

Thanks,

JT

Thanks Nicholas,
Before my first post I did a quick scan through WMI, and quickly got
lost, never having looked at it before, but I didn't see anything
relating to what I need. I will go look at your link and investigate
P/Invoke. I've never dealt with that either.
JT,
You ^might^ be able to do this through the WMI provider for windows,
which means you would access the System.Management namespace, but to be
honest, doing it through the P/Invoke layer is ^not^ that big of a deal.
The declaration for the function is at:
http://www.pinvoke.net/default.aspx/kernel32/SetThreadExecutionState.....
Also, you don't need unsafe code to call this. But a call through the
P/Invoke layer is required. In this case though, it's trivial. Some
sections of the .NET framework are completely reliant on the P/Invoke
layer
(pretty much everything in the System.Windows.Forms namespace).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I've realized that my good fortune of not having my computer go into
standby mode was the result of my application accessing a file on a
shared network drive. Not going into standby is a very good thing for
my application, but I don't want users to be required to share a drive
and file just to accomplish this.
I've read in old posts in various places stating that by importing
kernel32.dll I could access a function called
SetThreadExecutionState(...) with a value of ES_SYSTEM_REQUIRED,
potentially ORed with ES_CONTINUOUS (or the equivalent DWORD values),
would accomplish what I need. I'm guessing this is not managed code
and would have to be marshalled in and used as unsafe code (please
forgive me for not knowing anything about marshalling or unmanaged
code).
Is there a managed code way to accomplish this? Does anyone have any
suggestions?
Thanks,
JT- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
W

Willy Denoyette [MVP]

Hey Willy,

I've given the user a preference setting to let him decide how he
wants the application to behave. I figure if he's set it so that it
will not go into full standby, then if he purposely places it in that
mode, he has a reason for it. The application doesn't crash if it's
in standby.
The API only prevents the system to go into standby mode or turn the monitor
off when triggered by the "power mode policy" settings. This doesn't prevent
the user from manually putting the system in standby mode.

So what would a user do to cause standby mode when the application
setting has indicated otherwise?

Start menu - > Sleep or Press the Sleep or the Power button when configured
to put the system in Sleep state.

JT

Keep in mind that calling this API doesn't prevent a *user* to put the
system in Sleep state.

Willy.


Nicholas,

I just did my first P/Invoke and it works like a champ (on preliminary
evaluation)! Are there any caveats about P/Invokes?

Thanks,

JT

Thanks Nicholas,
Before my first post I did a quick scan through WMI, and quickly got
lost, never having looked at it before, but I didn't see anything
relating to what I need. I will go look at your link and investigate
P/Invoke. I've never dealt with that either.
JT,
You ^might^ be able to do this through the WMI provider for windows,
which means you would access the System.Management namespace, but to
be
honest, doing it through the P/Invoke layer is ^not^ that big of a
deal.
The declaration for the function is at:
http://www.pinvoke.net/default.aspx/kernel32/SetThreadExecutionState....
Also, you don't need unsafe code to call this. But a call through the
P/Invoke layer is required. In this case though, it's trivial. Some
sections of the .NET framework are completely reliant on the P/Invoke
layer
(pretty much everything in the System.Windows.Forms namespace).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I've realized that my good fortune of not having my computer go into
standby mode was the result of my application accessing a file on a
shared network drive. Not going into standby is a very good thing
for
my application, but I don't want users to be required to share a
drive
and file just to accomplish this.
I've read in old posts in various places stating that by importing
kernel32.dll I could access a function called
SetThreadExecutionState(...) with a value of ES_SYSTEM_REQUIRED,
potentially ORed with ES_CONTINUOUS (or the equivalent DWORD
values),
would accomplish what I need. I'm guessing this is not managed code
and would have to be marshalled in and used as unsafe code (please
forgive me for not knowing anything about marshalling or unmanaged
code).
Is there a managed code way to accomplish this? Does anyone have any
suggestions?
Thanks,
JT- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top