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

Hi,

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?

Thanks.
James.
JAMESAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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.
0
 
paulb1989Commented:
Could you not move the code that is to be executed after the delay into the timers event?
0
 
JAMESAuthor Commented:
Idle,

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.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.