Solved

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

Posted on 2011-03-24
7
287 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.Net Session Question 2 48
AJAX Create / Update Not working MVC 5 63
Beacon technology 3 28
Unable to Install .NET 3.5 on Windows 2012 server 4 11
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

734 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