Solved

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

Posted on 2011-03-24
7
283 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net/WinForms: adjust Screen Resolution 6 44
Group texting 7 50
Not showing JavaScript in the list 5 40
execute SQL Stored Procedure 1000 times from vb.net 3 26
Headphone jack broken? Do you notice the picture quality is better on a different device? If your Samsung Galaxy Note 3 is giving you headaches, this guide will run down a list of things you can do to improve its lifespan.   Phone Will Not Turn O…
Are you having trouble connecting or getting your iPhone / Samsung device(s) to sync with Microsoft Exchange Server?   What have you tried?   What haven't you tried?
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

19 Experts available now in Live!

Get 1:1 Help Now