[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Update a variable on screen inside a loop?

Posted on 2000-04-09
9
Medium Priority
?
195 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

834 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