How to break out of a loop using a chart event (Code attached)

 
Please guys look at the following code. It is meant to print numbers from 1000 down to 0. But I need to break out of the loop before it hits 0 by the press of a button. How can I achieve that? Control doesn't enter the loop.
void OnInit()
  {
   ObjectCreate(0,"Run",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"Run",OBJPROP_XDISTANCE,115);
   ObjectSetInteger(0,"Run",OBJPROP_YDISTANCE,25);
   ObjectSetInteger(0,"Run",OBJPROP_XSIZE,80);
   ObjectSetInteger(0,"Run",OBJPROP_YSIZE,25);
   ObjectSetString(0,"Run",OBJPROP_TEXT,"Click to run");
   ObjectSetInteger(0,"Run",OBJPROP_BGCOLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_COLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_COLOR, clrWhite);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"Run",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"Run",OBJPROP_STATE,false);
   ObjectSetInteger(0,"Run",OBJPROP_FONTSIZE,9);
  }

bool CheckClick = false;
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (CheckClick == false)
     {
      CheckClick = true;
     }
   else if (CheckClick == true)
     {
      CheckClick = false;
     }

   int i = 1000;
   do 
     {
      i = i - 1;
      Sleep(1000);
      Print("Next lower integer is ", i);
      if (CheckClick == true) break;
     }
   while (i > 0); 
  }
Custom Graphical Controls. Part 1: Creating a Simple Control
Custom Graphical Controls. Part 1: Creating a Simple Control
  • www.mql5.com
This article covers general principles of development of graphical controls. We are going to prepare tools for a quick and convenient work with graphical objects, analyze an example of creation of a simple control for entering text or numeric data as well as the ways of using it.
 

If you don't exit the event handler, you will not receive any new events.

Write your code to be event driven (for example, with a state machine) instead of holding up the execution with a loop.

 
Fernando Carreiro #:

If you don't exit the event handler, you will not receive any new events.

Write your code to be event driven (for example, with a state machine) instead of holding up the execution with a loop.

This is my event driven code with state of true or false. It still would not change state by clicking the button.


void OnInit()
  {
   EventSetMillisecondTimer(1);
   //------------------------------
   ObjectCreate(0,"Run",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"Run",OBJPROP_XDISTANCE,115);
   ObjectSetInteger(0,"Run",OBJPROP_YDISTANCE,25);
   ObjectSetInteger(0,"Run",OBJPROP_XSIZE,80);
   ObjectSetInteger(0,"Run",OBJPROP_YSIZE,25);
   ObjectSetString(0,"Run",OBJPROP_TEXT,"Click to run");
   ObjectSetInteger(0,"Run",OBJPROP_BGCOLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_COLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_COLOR, clrWhite);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"Run",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"Run",OBJPROP_STATE,false);
   ObjectSetInteger(0,"Run",OBJPROP_FONTSIZE,9);
  }

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (sparam == "Run" && id == CHARTEVENT_OBJECT_CLICK)
     {
      if (RunButton == true) 
        {
         RunButton = false;
        }
      if (RunButton == false) 
        {
         RunButton = true;
        }
     }
  }
  
bool RunButton = false;
int i = 1000;
void OnTimer()
  {
   if (RunButton == false)
     {
      Print ("Countdown paussed");
     }
   if (RunButton == true)
     {
      i = i - 1;
      Print("Next lower integer is ", i);
     }
  }
  
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
 
Fernando Carreiro #:

If you don't exit the event handler, you will not receive any new events.

Write your code to be event driven (for example, with a state machine) instead of holding up the execution with a loop.

I think I got it now. Thank you

void OnInit()
  {
   EventSetMillisecondTimer(1);
   //------------------------------
   ObjectCreate(0,"Run",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"Run",OBJPROP_XDISTANCE,115);
   ObjectSetInteger(0,"Run",OBJPROP_YDISTANCE,25);
   ObjectSetInteger(0,"Run",OBJPROP_XSIZE,80);
   ObjectSetInteger(0,"Run",OBJPROP_YSIZE,25);
   ObjectSetString(0,"Run",OBJPROP_TEXT,"Click to run");
   ObjectSetInteger(0,"Run",OBJPROP_BGCOLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_COLOR, clrBlue);
   ObjectSetInteger(0,"Run",OBJPROP_COLOR, clrWhite);
   ObjectSetInteger(0,"Run",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"Run",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"Run",OBJPROP_STATE,false);
   ObjectSetInteger(0,"Run",OBJPROP_FONTSIZE,9);
  }

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if (sparam == "Run" && id == CHARTEVENT_OBJECT_CLICK)
     {
      if (RunButton == true) 
        {
         RunButton = false;
        }
      else if (RunButton == false) 
        {
         RunButton = true;
        }
     }
  }
  
bool RunButton = false;
int i = 1000;
void OnTimer()
  {
   if (RunButton == false)
     {
      Print ("Countdown paussed");
     }
   if (RunButton == true)
     {
      i = i - 1;
      Print("Next lower integer is ", i);
     }
  }
  
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }