<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ICollection&#60;Notify&#62; &#187; System</title>
	<atom:link href="http://osmancelik.net/index.php/category/system/feed/" rel="self" type="application/rss+xml" />
	<link>http://osmancelik.net</link>
	<description>Change the World, this is your Future!</description>
	<lastBuildDate>Wed, 14 Apr 2010 21:29:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Prevent Standby</title>
		<link>http://osmancelik.net/index.php/csharp/prevent-standby/</link>
		<comments>http://osmancelik.net/index.php/csharp/prevent-standby/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 22:08:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[System]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[prevent standby]]></category>
		<category><![CDATA[standby]]></category>
		<category><![CDATA[Unsafe code]]></category>

		<guid isPermaLink="false">http://osmancelik.net/?p=95</guid>
		<description><![CDATA[One of my colleague ask me about how to &#8220;prevent standby&#8221; from csharp code. It obviously has nothing to do with csharp or any kind of .net languages. The real solution rely on Kernel32.dll SetThreadExecutionState API. Lets check out the msdn and write the code with Csharp. Msdn says for SetThreadExecutionState: Enables an application to [...]]]></description>
			<content:encoded><![CDATA[<p>One of my colleague ask me about how to &#8220;prevent standby&#8221; from csharp code.<br />
It obviously has nothing to do with csharp or any kind of .net languages.<br />
The real solution rely on Kernel32.dll SetThreadExecutionState API.<br />
Lets check out the msdn and write the code with Csharp.</p>
<p>Msdn says for <a href="http://msdn.microsoft.com/en-us/library/aa373208%28VS.85%29.aspx" target="_blank">SetThreadExecutionState</a>:<br />
Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.<br />
<strong>Return Value</strong><br />
If the function succeeds, the return value is the previous thread execution state.<br />
If the function fails, the return value is NULL.</p>
<p>Cool. Ok lets see what <a href="http://pinvoke.net/default.aspx/kernel32/SetThreadExecutionState.html" target="_blank">pinvoke.net</a> says about this.<br />
SetThreadExecutionState is used to stop the machine timing out and entering standby/switching the display device off.</p>
<p>Ok. Thats exactly what we want!</p>
<p>Prepare the enum, then [DllImport] attribute and then Prevent and Allow Methods.</p>
<pre class="brush: csharp; title: ; notranslate">
    public static class NativeCalls
    {
        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_SYSTEM_REQUIRED = 0x00000001,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_CONTINUOUS = 0x80000000,
        }

        //Do not forget to add the System.Runtime.InteropServices using directive
        [DllImport(&quot;kernel32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

        public static void PreventMonitorPowerdown()
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED |
                                EXECUTION_STATE.ES_CONTINUOUS);
        }

        public static void AllowMonitorPowerdown()
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
        }
    }
</pre>
<p>And read carefully the pinvoke&#8217;s note:<br />
<em>There is no need to store the state you set, Windows remembers it for you. Just set it back to ES_CONTINUOUS when you don&#8217;t want it anymore. Also note that this setting is per thread/application not global, so if you go to ES_CONTINUOUS and another app/thread is still setting ES_DISPLAY the   display will be kept on. Note that the return value is the EXECUTION_STATE that &#8221;was&#8221; set.</em></p>
<p>After all don&#8217;t forget the compile with -unsafe option.</p>
<hr />
<p><small>&copy; admin for <a href="http://osmancelik.net">ICollection&lt;Notify&gt;</a>, 2010. |
<a href="http://osmancelik.net/index.php/csharp/prevent-standby/">Permalink</a> |
<a href="http://osmancelik.net/index.php/csharp/prevent-standby/#comments">3 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://osmancelik.net/index.php/csharp/prevent-standby/&amp;title=Prevent Standby">del.icio.us</a>
<br/>
Post tags: <a href="http://osmancelik.net/index.php/tag/api/" rel="tag">API</a>, <a href="http://osmancelik.net/index.php/tag/os/" rel="tag">OS</a>, <a href="http://osmancelik.net/index.php/tag/prevent-standby/" rel="tag">prevent standby</a>, <a href="http://osmancelik.net/index.php/tag/standby/" rel="tag">standby</a>, <a href="http://osmancelik.net/index.php/tag/system/" rel="tag">System</a>, <a href="http://osmancelik.net/index.php/tag/unsafe-code/" rel="tag">Unsafe code</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://osmancelik.net/index.php/csharp/prevent-standby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

