Solved

windows mobile - best way to pause app to allow system to do its events...

Posted on 2011-03-24
7
280 Views
Last Modified: 2013-12-27
I ve been using System.THreading.Thread.Sleep to pasue to allow the phone to do its events,yet I was curious if there was a better way..this doesnt seem to be all that effective...
0
Comment
Question by:GlobaLevel
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:hjgode
ID: 35212898
System.Threading.Thread.Sleep() is the only right way to give the system time to process other processes and threads. But dont call Sleep() in your main GUI thread as it will block pending processing (http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/2ec4c3d8-bfc0-4e3c-a00f-52c8e317497c/). Use Sleep() only in background threads. Alternatively you can use waitEvent (WaitMultipleObjects) within background threads to let a background thread to not consume processor time.

If you like to give the .NET application the chance to process pending events, you may call Application.DoEvents(), see also http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx. DoEvents() is to be called inside event handlers to let the system process other events.

If you need to get actual data displayed, use Control.Refresh() and DoEvents().

Examples:
If your code needs to do a lengthy calculation, your code should create and use a background thread for the calculation code. As a background thread will run at full speed (keyword thread priority) and consumes processor time, you can periodically call Sleep() inside the background thread to give back processor time.
Another example: your code uses a windows.forms.timer to show the actual time. If you would call Thread.Sleep() in the timer event, the code will block and no other events, like button.Click for example, will be processed, That is what we call a bad design. As the user may click a button and nothing will happen.
When you call a server for getting some data, do this from a background thread and then fire an event to let the GUI code know when the data has been processed. Do not use Sleep() in main thread to wait for the data to be received as the GUI will block. And dont use Sleep inside the background thread to wait for the data.

Windows is event and message driven. A call to Sleep will block the thread from where the Sleep is issued. The thread will not get any events or messages as long as it is blocked.

Use Sleep only in background threads to let it suspend for some given time. Do not use a For Next loop to let the thread do nothing for some time, as this will run at full speed and consumes processor time slices (cooperative multithreading).
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35215863
from the article: "...This process will never wakeup and sit like the rock of Gibraltar on your desktop until you kill it with Task Manager. Here, the user should have used an event.."

>>yep that is exactly what is happening...his solution :
Private Sub WebBrowser_NavigationComplete(ByVal sender As Object, _
                         ByVal e As System.NavigationEventArgs) _
                                        Handles WbBrowser.NaVigationComplete

‘Process the data and continue

End Sub

 

...is foriegn to me as I have never had to program around event handlers before like his solution...not sure how to do the background threading thing..are there resources?
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35215892
...from his article, would you call this a background thread?
Private Sub WebBrowser_NavigationComplete(ByVal sender As Object, _
                         ByVal e As System.NavigationEventArgs) _
                                        Handles WbBrowser.NaVigationComplete

‘Process the data and continue

End Sub

...as it isnt aprt of the main thread...it will only kick off if the navigation is complete...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 16

Expert Comment

by:hjgode
ID: 35218074
Hi

no, this is not a thread it is an EventHandler. It handles the event NavigationComplete issued by the WebBrowser component.

The eventhandler should be part of the main thread so it can alter GUI elements directly.

You have or have to assign this eventhandler to the webbrowser's component NavigationComplete handler.

The code may issue the webbrowser's component Navigate method and the component calls back into your code by firing the event which is attached to your eventhandler when the browser component has completed to load the URL you specified with Navigate command.
You may issue a Cursor.Current=Cursors.WaitCursor before or after calling the non-blocking Navigate method. Then in the NavigationComplete handler set the Cursor back to Default. Possibly you may disable navigation controls until you receive the event.

The webbrowser component itslef will run a background thread to perform your navigate method. When the Navigate method completes or timed out the webbrowser code will issue the event to all subscribers.

Does this make sense?

0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35219141
...okay...so my guess...that function is called my some event triggered by the componet...bc its not being code inherently in the code..thus windows is event driven..so the Operating system triggers it..it seems there is something going on outside the code...

Private Sub WebBrowser_NavigationComplete(ByVal sender As Object, _
                         ByVal e As System.NavigationEventArgs) _
                                        Handles WbBrowser.NaVigationComplete

‘Process the data and continue

End Sub
0
 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
ID: 35220865
So you are looking for how to write and interact with background threads.

Here is an article by MS: http://msdn.microsoft.com/en-us/library/aa446488.aspx

I have written an easy to use background thread class and published it here http://www.hjgode.de/wp/2010/06/01/mobile-development-easy-to-use-background-thread-with-gui-update/

And there are many, many more sources in internet for (background) thread programming

Some notes:
+ Additional threads are to be used to work in the background
+ They run async to main thread
+ in .net you have to stop the threads before you are able to exit your app
+ threads can inform the GUI or other threads for status with the use of events
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35244852
thank you...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now