The following sample projects illustrate the syntax, structure, and techniques used to solve various device programming challenges.
In This Section
- Smart Device Samples
-
Provides already-built projects for inspection.
- Smart Device Walkthroughs
-
Provides step-by-step instructions on how to create different types of applications and components. Includes both managed and native projects.
Related Sections
See Also
Using P/Invoke, you can disable the suspend mode in a Compact Framework smart device application.
Use this native import :
[C#]
[DllImport("coredll.dll")]
internal extern static void SystemIdleTimerReset();
More informations on this method are available here : http://msdn.microsoft.com/library/en-us/wceui40/html/cerefSystemIdleTimerReset.asp?frame=true
This native method reset the internal timer that detect the idle timeout.
You can use the following code the create a Power class that help you to manager the suspend enabling and disabling.
[C#]
using System;
using System.Runtime.InteropServices;
using System.Threading;
/// <summary>
/// Power manage the ability of the device to go into suspend mode
/// </summary>
public sealed class Power
{
private Power() { }
private static int _suspendedRequestNumber; // Runtome default = 0;
/// <summary>
/// Gets the number of suspended request. If the result is 0, the device can sleep.
/// </summary>
/// <value>The suspended request number.</value>
public static int SuspendedRequestNumber
{
get { return Power._suspendedRequestNumber; }
}
/// <summary>
/// DisableSuspend() method set the device not to go into suspend mode. Call
/// EnableSuspend to reverse
/// </summary>
public static void DisableSuspend()
{
if (_suspendedRequestNumber++ == 0)
{
ThreadStart ts = new ThreadStart(
Loop
);
Thread t = new Thread(ts);
t.Start();
}
}
/// <summary>
/// EnableSuspend method enable the device to go into suspend mode.
/// </summary>
public static void EnableSuspend()
{
EnableSuspend(false);
}
/// <summary>
/// EnableSuspend method allow the device to go into suspend mode.
/// </summary>
/// <param name="reenableAllSuspendRequests">if set to <c>true</c> reenable all suspend requests.</param>
public static void EnableSuspend(bool reenableAllSuspendRequests)
{
if (reenableAllSuspendRequests)
{
_suspendedRequestNumber = 0;
}
else if(_suspendedRequestNumber == 0)
{
_suspendedRequestNumber--;
}
}
private static void Loop()
{
while(_suspendedRequestNumber > 0)
{
NativeMethods.SystemIdleTimerReset();
System.Threading.Thread.Sleep(
20 * 1000
);
}
}
}
/// <summary>
/// Provides native methods
/// </summary>
internal sealed class NativeMethods
{
[DllImport("coredll.dll")]
internal extern static void SystemIdleTimerReset();
}
This class use a second thread to periodically reset the idle timer. the _suspendedRequestNumber hold the number of calls to the DisableSuspend in order to allow multiple threads to query the DisableSuspend and EnableSuspend without conflicting each others.