OnStop method

Created at 23 Apr 2021, 00:17
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!
swingfish's avatar

swingfish

Joined 25.06.2016

OnStop method
23 Apr 2021, 00:17


when i put async call in OnStop it not getting executed because the algo quites before its complete, 

 

I tried System.Threading.Thread.Sleep but it still stops immediately,

 

is there a way to make sure the bot "waits" a but before exiting to let it send the final commands ?

 

 


@swingfish
Replies

amusleh
23 Apr 2021, 10:21

Hi,

Async functions are none blocking functions, if you call an async function it will not block the current thread and the thread can continue executing next line of code or commands.

Whenever the OnStop code execution is finished the cBot will stop, so to solve this you can either use synchronous methods or use a thread blocking technique like:

using cAlgo.API;
using System.Threading;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Robot
    {
        private bool _stop;

        protected override void OnStart()
        {
        }

        protected override void OnStop()
        {
            Print("On Stop Start");

            var thread = new Thread(() =>
            {
                Thread.Sleep(5000);

                _stop = true;
            });

            thread.Start();

            while (_stop == false)
            {
                Thread.Sleep(1000);
            }

            Print("On Stop Finished");
        }
    }
}

The thread sleep method works fine, on my sample code I block the current thread until the other thread set the "_stop" field value to true after 5 seconds.

You can wait for your async method callbacks the same way.


@amusleh

swingfish
23 Apr 2021, 16:14

Sorry i was not clear abou the issue at hand,

the calgo communicates with another device, which all works perfectly, I want to "inform" the other device that the bot is stopping

 

so in OnStop i sent a command to do so, the problem is the other device doesn't accept the Information if the connection is not completed.

when I click the stop button, the calgo sends the Async command, and then kills itself but the killing also severed the async connection so there is no ACK for the last transmission.

 

a workaround would be just to wait 500-1000 ms 

 


@swingfish

amusleh
23 Apr 2021, 16:27

RE:

swingfish said:

Sorry i was not clear abou the issue at hand,

the calgo communicates with another device, which all works perfectly, I want to "inform" the other device that the bot is stopping

 

so in OnStop i sent a command to do so, the problem is the other device doesn't accept the Information if the connection is not completed.

when I click the stop button, the calgo sends the Async command, and then kills itself but the killing also severed the async connection so there is no ACK for the last transmission.

 

a workaround would be just to wait 500-1000 ms 

 

Hi,

You can wait for the async method to complete by putting the thread on sleep and checking with a while loop like I did on previous example.


@amusleh

swingfish
24 Apr 2021, 18:30

RE: RE:

amusleh said:

 

Hi,

You can wait for the async method to complete by putting the thread on sleep and checking with a while loop like I did on previous example.

your example code did not prevent the calgo from shutting down ..


@swingfish

amusleh
24 Apr 2021, 19:46 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE: RE:

swingfish said:

amusleh said:

 

Hi,

You can wait for the async method to complete by putting the thread on sleep and checking with a while loop like I did on previous example.

your example code did not prevent the calgo from shutting down ..

It does:

The cBot stopped approximately 5 seconds after OnStop method is called as you can see on the cBot logs.


@amusleh