Friday, September 7, 2012

Are there any alternatives to Sleep() for Silverlight for wince7?

Are there any alternatives to Sleep() for Silverlight for wince7?

I am working on a Silverlight for Windows Embedded project and running into a problem with my UI thread. Currently the application UI thread must continually update its display based on data stored in a shared memory file and then sleep for a specified number of milliseconds before updating the UI once again. So in essence the code is something like the following:

while(true){     UpdateUI();    //gets data from shared memory and updates graphics     Sleep(250); } 

However, as I am sure some of you know, it is not recommended to use Sleep() within the main Silverlight UI thread. Sure enough, my entire embedded system will crash after running for 20-30mins and I have indentified the call to Sleep() as the issue.

What I need is an alternative method to update the UI, pause for some amount of time, and then update the UI again, repeating this process ad infinitum.

As far as I am aware, the embedded nature of the project does not allow me to use some of the obvious alternatives to Sleep() that would be a better solution in the UI thread (i.e. Join()).

Any suggestions would be greatly appreciated!

Answers & Comments...

Answer: 1

A timer is a far better route then a loop, especially a loop with a Sleep in the UI thread. A simple call to SetTimer or timeSetEvent is probably what you're after.

int WinMain(...) {     // initialize xamls stuff      // start a timer     SetTimer(0, 0, 250, TimerProc);     // *OR*     timeSetEvent(250, 0, EventProc, NULL, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);      // call StartDialog or whatever to launch your UI }  void CALLBACK TimerProc(HWND hwnd, UINT, UINT, DWORD dwTime) {     // this will get called every 250ms         UpdateUI(); }  void CALLBACK EventProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) {     // this will get called every 250ms         UpdateUI(); } 
by : ctackehttp://stackoverflow.com/users/13154




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog