LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Stop while loop with button

Hello All,

 

After I wend through the course book of CVI I started to make a simple program.

First I make 3 buttons, START, STOP and QUIT.

The I generate the code.

In the START callback I place a while loop with condition running, this is set '1' when the while loop is started.

In de While loop I place ProcessSystemEvents ();.

in the STOP callback I set the running variable to 0, and the same in the QUIT callback.

(running is set as global variable, statis int running;)

 

This performed the same as the example showed in the course book...

 

However when hitting the START button several times I get an error.

"The program has caused a 'General Protection' fault at ......"

 

Is this a known problem since I must trust the course book?

 

What do I have to correct or do different?

 

gr,

 

 

 

int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "gravity.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}


int CVICALLBACK StartGravity (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{

switch (event)

{
case EVENT_COMMIT:running = 1;

while (running)
{

Delay (0.1);
ProcessSystemEvents ();

}

break;
case EVENT_RIGHT_CLICK:

break;
}
return 0;
}

int CVICALLBACK StopGravity (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
running = 0;
break;
case EVENT_RIGHT_CLICK:

break;
}
return 0;
}

 

int CVICALLBACK QuitProgram (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
running = 0;
QuitUserInterface (0);
break;
case EVENT_RIGHT_CLICK:

break;
}
return 0;
}

0 Kudos
Message 1 of 5
(4,293 Views)

You should not press Start button several times: the button is honoured since you have the ProcessSystemEvents in the loop but you my expect the error when trying to run the loop several times.

One possible solution is to dim the button at the very entrance in the callback and enable it again when exit: this will block user actions on the button while running the test.

 

int CVICALLBACK StartGravity (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
  switch (event) {
    case EVENT_COMMIT:
      SetInputMode (panel, control, 0);
      running = 1;
      while (running) {
        Delay (0.1);
        ProcessSystemEvents ();
      }
      SetInputMode (panel, control, 1);
      break;
}
return 0;
}

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 5
(4,276 Views)

Hi Robert,

 

That looks good, but I may go for the timer style.(like an example I found yesterday).

 

Still I don't get it why it is stated in the course book and I didn't see any comment saying that this error will happen.

 

Bad example NI.

 

gr,

0 Kudos
Message 3 of 5
(4,270 Views)

The examples in the course book are designed to help you understand concepts. They are intentionally simplified in many cases to make things easier to understand. It does not surprise me that some of the examples may not be very robust. I would recommend using the examples from the Example Finder instead of the course manual. They are more thoroughly tested and complete than those in the course manual which serve a singular purpose. But thank you for the feedback about the error, I will investigate a method to include this in our next edition of the manual.

National Instruments
0 Kudos
Message 4 of 5
(4,251 Views)

I second D Biel opinion: tutorial samples are over simplified and should not be taken as a starting point for real applications. Moreover, each of them is aimed to a specific concept leaving other aspects out of consideration.

Just as an example, you should not use a control callback for long-lasting functions like the schema depicted in this sample.

Take these as the first lessons in your personal training course that will have several more ones Smiley Wink

 

But I'm curious about your 'go for the timer style' sentence: can you illustrate a bit your solution?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 5
(4,246 Views)