Whats the best way to send my app to sleeping waiting for a condition without it becoming unresponsive?

Posted on 2006-05-10
Medium Priority
Last Modified: 2010-04-16

I have a function in my app that checks the condition of an external app.  When this condition is met my app can carry on through its sequence of events.

I have used thread.sleep(1000) in a self calling loop but my app becomes unresponsive so I decided a system.timer is the way forward but because of their very asynchronous nature my app wants to continue through it's sequence when my condition is not yet met.

I know I could set a flag within timer event but then i would have to be in a constant loop checking this flag!

Any thoughts?

Question by:JAMES

Expert Comment

ID: 16650280
Could you not move the code that is to be executed after the delay into the timers event?
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16650672
Use a Class to encapsulate a Thread that simply checks the status of your external app.  The loop in the thread can safely call Sleep() without affecting your main UI.

When the condition is met, raise an Event from your class.  The main UI should subscribe to this event and act accordingly.

You will need to modify your UI so that the user cannot progress forward until the event has been raised.  This is usually accomplished by disabling (or hiding) controls that you do not want the user to use.

Since your polling loop is on a different thread, you will need to use a Delegate and the Invoke method to marshal the event onto your main UI thread before continuing.

Author Comment

ID: 16655635

Lovely solution!

My main loop was already in another class so it was a simply case of launching that in a a new thread and all works pefectly.

Thank you.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

862 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