Thread.Sleep and synchronizing threads
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Thread.Sleep and synchronizing threads

  1. #1
    Join Date
    Oct 2009
    Posts
    8

    Thread.Sleep and synchronizing threads

    I'm trying to build a simple alarm clock application that shows a MessageBox at a particular time. I've been trying to teach myself about threading, but I'm having a hard time putting my finger on what I need (since there's so many different ways to do everything, it seems). There are a ton of tutorials out there, but I haven't stumbled upon any good ones. I understand the basics of running threads, but I don't understand how to do inter-thread communication (with everything running asynchronously)

    Right now, I have a while loop that checks to see if the system time matches the alarm time.

    Obviously, it runs the CPU at 100% all the time. So I tried putting Thread.Sleep() in the loop, but obviously, that just locks up the UI. If I start the while loop on its own thread, it can't access the alarm control's value (or at least, I can't figure out a way to get it to do so). If I were to pass that information to the thread in some way, how could I update the thread if the alarm control changes? I'm currently using the TimePicker from the Avalon control library, which has generous amounts of properties and event handlers. Could someone help me understand how to use threading in this case? Or is there a much simpler way of doing this?

    I appreciate anyone's help or suggestions!

  2. #2
    Join Date
    Oct 2009
    Posts
    1

    Re: Thread.Sleep and synchronizing threads

    Here's a very good article that talks about what's required to update a UI control from another thread: http://weblogs.asp.net/justin_rogers/pages/126345.aspx

    If you pass a reference to your main UI class to the worker thread when you start it up, you have a way to refer back to the controls in the UI.

    For example:

    Code:
            this.queueServ = new QueueServicer(this);
            this.queueServiceThrd = new Thread(new ThreadStart(this.queueServ.ServiceQueue));
            this.queueServiceThrd.Start();
    QueueServicer is the class that I wrote to implement the worker thread in this example (the particular code I took the example from deals with servicing a queue maintained by the main thread). One of the things it contains is a method called ServiceQueue which is the actual thread proc for the worker thread. As you can see, an instance of the UI class is passed in the constructor for the worker thread class. This allows the worker thread to get access back to the main UI.

    IMPORTANT!
    You cannot simply update the UI controls using the passed in class reference. You have to use the methods described in the article above to avoid cross-threading problems.

    Another article I found: http://www.codeproject.com/KB/cs/workerthread.aspx

    One more: http://social.msdn.microsoft.com/For...8-32381aed2ccd

    I hope this helps, and good luck!

    Tom

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,476

    Re: Thread.Sleep and synchronizing threads

    Funka...., please post the code that you have so far so we can take a look at it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center