Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Update a variable on screen inside a loop?

Posted on 2000-04-09
9
Medium Priority
?
194 Views
Last Modified: 2010-04-02
Edited:
I have solved the problem below, but new problem exist :
How to gain control for the OK or CANCEL button while Button1 is pressed and in the loop?


I want the variable to be updated continuously after I pressed a button. However, I can't do that with UpdateData(false) which placed inside a loop. Anyone know the solution?

My sample code:

void CAaaDlg::OnButton1()
{
      while (1==1)
            {
            m_a=m_a+1;
            UpdateData(false);
                   UpdateWindow();
            }
}
0
Comment
Question by:mingming_ew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 2699382
The problem is that the message loop is not reached while you are inside the loop
may be the function RedrawWindow will help
or you have to build your own MessageLoop:
MSG msg;
 
if(GetMessage(&msg,NULL,0,0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
This will enebls your program to handle messages when it is inside the loop
0
 

Author Comment

by:mingming_ew
ID: 2699443
I solved this problem by UpdateWIndow()

waiting solution for quitting the loop by pressing button
0
 
LVL 1

Expert Comment

by:pepenieto
ID: 2699531
I think that when you press the button, you must create a new task that will update the variable continuosly:

h=CreateThread(NULL,10000,<your function name>,NULL,NULL,&ThreadId);

....

and the function:

unsigned long _stdcall <function name>(void*t) {
  while(1) {
    UpdateData(false);
    // if you watn to kill this task in one moment, check here a semaphore with <WaitForSingleObject> and then <return 0;>
  }

This, you will have a task that only update the variables, but I can´t find utility for this because you can call UpdateData when you want this update.



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:ch52jb
ID: 2699563
If you want to trap the messages from the ok and cancel buttons, you need to do what norbert suggests.  This will allow the messages to be used.  What you can also do is add an event to the buttons to set a member variable, which you can monitor from inside the loop.
0
 
LVL 3

Expert Comment

by:Norbert
ID: 2700630
Windows is an event driven system
The Operating system shedules the incomming events to the program that hosts the window where the event occures.
But it does not send them! The program have to poll for new events
so you have to call GetMessage or may be PeakMessage because GetMessage will wait until a message occures
you are using MFC so this Message Loop is hidden inside the deep of MFC
but it is still there.
If you Don't ask events are stored inside the queue but never tomething will happens
0
 

Author Comment

by:mingming_ew
ID: 2700792
Thanks for the input of everyone. However, I am a novice in VC++ and can't truly understand you guys valuable comment.

My program just have 2 button, one for quit, one for start counting.
m_a will keep counting in a loop after you press Button1 and will allow you to stop when you press Quit.

Can anyone post the code here? Coz I can't quite understand some of the comment above.
I though pepenieto's ans is close to what I want, but I dunno how to implement.
0
 
LVL 8

Accepted Solution

by:
mrblue earned 80 total points
ID: 2700831
Try this:

void CAaaDlg::OnButton1()
{
  MSG msg;
  m_bDooLoop = TRUE;

  while (m_bDooLoop) {
    m_a=m_a+1;
    UpdateData(false);

    while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    }
  }
}

void CAaaDlg::OnButton2()
{
  m_bDooLoop = FALSE;  
}

Where
  m_bDooLoop is member of your window class
0
 
LVL 1

Expert Comment

by:pepenieto
ID: 2700991
Here´s the code:
unsigned long __stdcall Increment(void*t) {
extern CDlg dlg;
      while (1) {
            res=WaitForSingleObject(m_Semaforo,5);
            if (res!=WAIT_OBJECT_0) {
                  // do something like...
                  dlg.Increment();
            } else {
                  break;
            }
      }
      return 0;
}

void CDlg::Increment() {
  m_var++;
}

void CDlg::Button1() {
      DWORD ThreadId;
      if (!m_switch) {
            h=CreateThread(NULL,10000,Increment,NULL,NULL,&ThreadId);
            m_switch=1;
      }
}

void CDlg::Button2() {
      if (m_switch) {
            ReleaseSemaphore(m_Semaforo,1,NULL);
            m_switch=0;
      }
}

.....

The next are class members (of CDlg):
      HANDLE m_Semaforo;
      int m_switch;
      m_var;

And the method CDlg::Increment() will be public.

This you create a new task that do all the work that you want to do in background, and at the same time the window process all messages (of course, if Button2 is pressed, the code associated will be executed) and you don´t have to write the process of the messages again.
0
 
LVL 1

Expert Comment

by:pepenieto
ID: 2701011
Sorry, but CDlg::Increment() does not exist. It´s a function, not a method member of CDlg.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

609 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