Link to home
Start Free TrialLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on

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

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...
Avatar of hjgode
hjgode
Flag of Germany image

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).
Avatar of GlobaLevel

ASKER

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?
...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...
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?

...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
ASKER CERTIFIED SOLUTION
Avatar of hjgode
hjgode
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you...