Search

Wednesday, September 1, 2010

get timezone change notification in windows mobile compact framework?

want to detect time changes or timezone changes events ? people who is working on .netcf windows mobile sdk knows very well that something that should be straight is never straight and something which should be hidden is open, one such catch in windows mobile development is once the application or process starts there is no way we can know if the timezone has changed, even thought the timezone has changed the date time api always returns old timezone value unless until the process/application is restarted.

recently i came across such a situation where i needed to handle timezone change case so after a lot of R&D i came across few sample code snippets in msdn and other discussion forums and websites which showed we can achieve this using win32 interop api's below is the tailored working code.

private const int NOTIFICATION_EVENT_TIME_CHANGE = 1;

private const UInt32 INFINITE = 0xFFFFFFFF;




[DllImport("coredll.dll", EntryPoint="CeRunAppAtEvent",

SetLastError=true)]

private static extern int CeRunAppAtEvent(string pwszAppName,

NOTIFICATION_EVENT lWhichEvent);

[DllImport("coredll.dll", EntryPoint="CreateEvent",SetLastError=true)]

private static extern IntPtr CreateEvent(IntPtr security, int

manualreset, int initialstate, string name);

[DllImport("coredll.dll",

EntryPoint="WaitForSingleObject",SetLastError=true)]

private static extern int WaitForSingleObject(IntPtr handle, uint

millisec);

[DllImport("coredll.dll", EntryPoint = "CloseHandle", SetLastError =

true)]

private static extern bool CloseHandle(IntPtr hHandle);




public Form1()

{

InitializeComponent();

CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents \\MyTimeNamedEvent",

NOTIFICATION_EVENT_TIME_CHANGE);

Thread thread = new Thread(new ThreadStart(ThreadProc));

thread.Start();

}





private void ThreadProc()

{

IntPtr hEvent = CreateEvent(IntPtr.Zero, false, false,

"MyTimeNamedEvent");

WaitForSingleObject(hEvent, INFINITE);

MessageBox.Show("Time Event Occured");

CloseHandle(hEvent);

}


above code worked flawlessly for me on almost all the devices from pocketpcs to smartphone if you know still better approach do let us know.


1 comments:

Anonymous said...

Came across your blog while searching for blogs on science in Indiblogger. You have a nice blog here. Blogrolling you. Will be back for more :)

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google