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

Posted on 2006-05-10
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
    LVL 5

    Expert Comment

    Could you not move the code that is to be executed after the delay into the timers event?
    LVL 85

    Accepted Solution

    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


    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    732 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

    20 Experts available now in Live!

    Get 1:1 Help Now