Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-24
7
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let Bitmoji into your life. Now is the time to learn a new language of smartphone messaging with this brief introduction.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

663 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