onTimer using problem

Created at 27 Sep 2014, 17:21
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!
neverman's avatar

neverman

Joined 06.07.2014

onTimer using problem
27 Sep 2014, 17:21


Hello,

I got a problem starting my script with different onTimer milliseconds. On 300 milliseconds and more, the script works well. But If I decrease the milliseconds, the cTrader crash.

Problem signature:
  Problem Event Name:    CLR20r3
  Problem Signature 01:    ctrader.exe
  Problem Signature 02:    1.24.102.34377
  Problem Signature 03:    53ff6fe2
  Problem Signature 04:    mscorlib
  Problem Signature 05:    4.0.0.0
  Problem Signature 06:    4ba1da6f
  Problem Signature 07:    214
  Problem Signature 08:    10
  Problem Signature 09:    System.ArgumentException
  OS Version:    6.1.7601.2.1.0.256.1
  Locale ID:    1026
  Additional Information 1:    0a9e
  Additional Information 2:    0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:    0a9e
  Additional Information 4:    0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

 

Generally the script read the clipboard and write it into csv file. On 300 milliseconds (or more slowly - 1 sec for example) onTimer, it works properly and keep the data as planned. But faster, it can't ... Any ideas where to find my problem?


@neverman
Replies

neverman
27 Sep 2014, 20:39

I decided to decrease the amount of calculations on the script, and this allowed me to decrease the milliseconds to 25. Now another error appear.

Problem signature:
  Problem Event Name:    CLR20r3
  Problem Signature 01:    ctrader.exe
  Problem Signature 02:    1.24.102.34377
  Problem Signature 03:    53ff6fe2
  Problem Signature 04:    mscorlib
  Problem Signature 05:    4.0.0.0
  Problem Signature 06:    4ba1da6f
  Problem Signature 07:    3dab
  Problem Signature 08:    1f4
  Problem Signature 09:    System.IO.IOException
  OS Version:    6.1.7601.2.1.0.256.1
  Locale ID:    1026
  Additional Information 1:    0a9e
  Additional Information 2:    0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:    0a9e
  Additional Information 4:    0a9e372d3b4ad19135b953a78882e789


 I hope this info will help to give me idea for a solution.


@neverman

Spotware
29 Sep 2014, 12:20

Do you use cAlgo OnTimer method or system timer?


@Spotware

neverman
29 Sep 2014, 15:07

RE:

Spotware said:

Do you use cAlgo OnTimer method or system timer?

Here you are the code:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Globalization;
using System.IO;

using System.Windows.Forms;
using System.Threading;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.FullAccess)]
    public class testClipboard : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }


        protected override void OnStart()
        {


            Timer.Start(TimeSpan.FromMilliseconds(1));
            //start timer with minimal interval

            //Timer.Start(1);
            //start timer with 1 second interval
        }

        protected override void OnTimer()
        {
            copy_to_clipboard();

        }


        public void somethingToRunInThread()
        {


//############# clipboard work ################

            string ClipboardData = "";
            double AskEUR_CT = 0;
            double BidEUR_CT = 0;



            Symbol EUR = MarketData.GetSymbol("EURGBP");
            Symbol USD = MarketData.GetSymbol("GBPUSD");
            Symbol CHF = MarketData.GetSymbol("GBPCHF");
            Symbol JPY = MarketData.GetSymbol("GBPJPY");

            AskEUR_CT = EUR.Ask;
            BidEUR_CT = EUR.Bid;

            ClipboardData = AskEUR_CT.ToString("0.00000") + "," + BidEUR_CT.ToString("0.00000") ;
            Clipboard.Clear();
            Clipboard.SetText(ClipboardData);


        }

        protected void copy_to_clipboard()
        {
            Thread clipboardThread = new Thread(somethingToRunInThread);
            clipboardThread.SetApartmentState(ApartmentState.STA);

            clipboardThread.Start();
        }



        protected override void OnStop()
        {

            //  Timer.Stop();
        }


    }
}

its a little changed from the first version (do not write to csv file), but the error is the same. Obviously its related to clipboard work. And I'm using OnTimer method.


@neverman

Spotware
29 Sep 2014, 15:20

cAlgo.API is not thread safe. Please avoid accessing API objects and methods from separate threads. If exception happens, the entire application will crash.


@Spotware

neverman
29 Sep 2014, 15:42

RE:

Spotware said:

cAlgo.API is not thread safe. Please avoid accessing API objects and methods from separate threads. If exception happens, the entire application will crash.

Not a big help for the moment ... like "Doctor, when I touch here I feel pain ... so don't touch here!"


@neverman

Spotware
29 Sep 2014, 15:51

We can recommend you 2 things:

  • add a try catch statement to the somethingToRunInThread method. It is the best practice to add try catch block to the separate thread even if you think it is safe.
  • obtain all required data in the main thread (OnTimer method) and then pass it to somethingToRunInThread method using any synchronization mechanism

@Spotware

Researcher
29 Sep 2014, 23:03

Why do you create thread in OnTimer() method? I'd recommend you just to your job in robot's thread right in OnTimer() method.

Consider one optimization: Do not call Clipboard.Clear() and Clipboard.SetText(ClipboardData) if clipboard data is the same as you set in previous iteration.


@Researcher

gunning.ernie
24 Dec 2017, 07:30

RE:

Spotware said:

cAlgo.API is not thread safe.....

Is this still the case today? I'm asking cause I use the Parralel libraries like parallel.For...?


@gunning.ernie

PanagiotisCharalampous
27 Dec 2017, 14:55

Hi gunning.ernie,

Yes it is still the case.

Best Regards,

Panagiotis


@PanagiotisCharalampous