Thread: Loops until I click or unclick a button

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    37

    Loops until I click or unclick a button

    Hello. I would like to ask the following question.

    I am using Visual C++ express and building a windows form application. There is a button (actually a checkbox in the appearance of a button) among other things in my Form.

    What I want to do is for some function to be called repeatedly whenever the checkbox is ON. and to stop whenever I mark it OFF.

    I tried putting an endless loop(while checked==true) inside the CheckedChange function of the checkbox but predictably it locked the application and couldnt call it off.

    So I am thinking to put a Timer that will call the function as long as the timer is active.

    Does any of you have any other idea of how to realize this? Any advice will be greatly appreciated

    Kansai

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1. This is really a Windows question (really a WinForms question), not a C++ question; the Windows forum would have been a better choice.

    2. You can't do a lot of intensive processing on the GUI thread. Firstly, you should evaluate how often this code really needs to fire (is it really something that needs to run "continuously", or is running it periodically fine)?

    If you really need this to run many, many iterations per second, you probably should spawn a background thread and use the checkbox to spawn the thread or signal the thread to terminate.

    If you want this to just run at intervals, a timer object is fine.

    What kind of function are you actually calling? There may be an even better way to do this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Windows programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Thank you for the answer (and the move, sorry about that)

    Well I tried it with the timer and it works fine so far but Cat you just remind me of multithreading. As far as I know the libraries that I am using are thread-safe so maybe I will use that since the functions needs to be called constantly (it is a image grab function from a camera) . But I suppose I will leave that for later. Thanks again! that was illuminating.

    Kansai

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forms - Button Click
    By gavra in forum C# Programming
    Replies: 1
    Last Post: 11-17-2009, 01:13 AM
  2. Button click out of WM_COMMAND
    By Sober in forum Windows Programming
    Replies: 8
    Last Post: 05-03-2007, 09:07 PM
  3. button click
    By Stringer in forum C++ Programming
    Replies: 20
    Last Post: 10-17-2006, 07:00 AM
  4. Updating window after button click?
    By ac251404 in forum Windows Programming
    Replies: 0
    Last Post: 07-18-2006, 10:20 AM
  5. How To Change Caption's On A Button When It Is Click ?
    By SonicWave in forum Windows Programming
    Replies: 1
    Last Post: 09-19-2001, 02:14 PM