Create a Timer In indicator

Created at 09 Apr 2020, 14:50
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
bienve.pf's avatar

bienve.pf

Joined 19.09.2018

Create a Timer In indicator
09 Apr 2020, 14:50


Hello, I have tried to make a stopwatch independent of the ticks using the "Task" or "System.Threading.Timer" instruction in the Initialize function of an indicator. I have classified the variable that refers to the Task or thread as "static".

Both methods do not last. They stop after a few minutes.

How can I make a reliable counter inside an indicator without relying on ticks?

Regards.

 

 

public static System.Threading.Timer aTimer;
public static Task task = null;

bool Finished = false;
protected override void Initialize()
{

  aTimer = new System.Threading.Timer(TimerCallback2, null, 1000, 50);
  
  task = new Task(() =>
  {
      while (!this.Finished)
      {
          this.TimerCallback(null);
          System.Threading.Thread.Sleep(10);
      }
  });
  task.Start();

}






 


@bienve.pf
Replies

firemyst
28 May 2020, 15:49

RE:

bienve.pf said:

Hello, I have tried to make a stopwatch independent of the ticks using the "Task" or "System.Threading.Timer" instruction in the Initialize function of an indicator. I have classified the variable that refers to the Task or thread as "static".

Both methods do not last. They stop after a few minutes.

How can I make a reliable counter inside an indicator without relying on ticks?

Regards.

 

 

public static System.Threading.Timer aTimer;
public static Task task = null;

bool Finished = false;
protected override void Initialize()
{

  aTimer = new System.Threading.Timer(TimerCallback2, null, 1000, 50);
  
  task = new Task(() =>
  {
      while (!this.Finished)
      {
          this.TimerCallback(null);
          System.Threading.Thread.Sleep(10);
      }
  });
  task.Start();

}






 

Why not just use a StopWatch object? The only timer I think you can reliably use is the built in timer within the cAlgo API.


@firemyst