BeginInvokeOnMainThread

Created at 17 Dec 2015, 18:34
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!
ClickAlgo's avatar

ClickAlgo

Joined 05.02.2015

BeginInvokeOnMainThread
17 Dec 2015, 18:34


Spotware,

Do you having an example of using the method BeginInvokeOnMainThread with cAlgo, the only examples can find are with xamarin

https://developer.xamarin.com/api/member/Xamarin.Forms.Device.BeginInvokeOnMainThread/p/System.Action/

Can this be used with the  the Task Parallel Library (TPL)

Regards,

Paul.


@ClickAlgo
Replies

ClickAlgo
17 Dec 2015, 18:42

Forget the Tasks question, just information on the BeginInvokeOnMainThread , is it the same usage as xamarin examples.


@ClickAlgo

moneybiz
18 Dec 2015, 21:57

ExecutionContext.Run may help.

What do you want to achieve? Can you be more specific?

At first I thought you wanted to execute code via BeginInvoke of the cAlgo window.
Unfortunately I couldn't succeeded because I couldn't convert the window handle to System.Windows.Forms.Control object. Probably because cAlgo window is not System.Windows.Forms.Control type object.

Anyway, here is the code.

 

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using cAlgo.API;

namespace MoneyBiz.Trader.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.FullAccess)]
    public abstract class ControlIndicator : Indicator
    {
        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            var executionContext = ExecutionContext.Capture();
            ExecutionContext.Run(executionContext, state =>
                                                   {
                                                       File.WriteAllLines(@"D:\Test.txt", new[] { string.Format("index: {0}", index) });
                                                   }, null);
        }

        private void FindWindowInvoke(int index)
        {
            var handle = FindWindow(null, "Spotware cAlgo");
            if (handle == IntPtr.Zero)
            {
                Print("Couldn't find cAlgo window.");
                return;
            }

            // control is always null, the reason could be because handle is not from System.Windows.Forms.Control instance.
            var control = Control.FromHandle(handle);
            if (control == null)
            {
                return;
            }

            Action run = () =>
                         {
                             File.WriteAllLines(@"D:\Test.txt", new[] { string.Format("index: {0}", index) });
                         };

            control.BeginInvoke(run);
        }


        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);
    }
}

 


@moneybiz

ClickAlgo
18 Dec 2015, 22:03

take a look at this old email, spotware have added this new method since then. have you used the cAlgo API method BeginInvokeOnMainThread ?

/forum/cbot-support/3155

 


@ClickAlgo

Spotware
21 Dec 2015, 20:33

Dear Trader,

Please have a look at the following example:

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

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

        protected override void OnStart()
        {
            var asyncTask = new System.Threading.Tasks.Task(() =>
            {
                //this code will be executed on a separate thread

                var response = HttpGet("http://spotware.com");
                // perform http get request (in a separate thread)
                //you have to access API members only from the main thread
                // always use BeginInvokeOnMainThread if you work with other threads
                BeginInvokeOnMainThread(() => HandleResponseInMainThread(response));
            });
            asyncTask.Start();
        }

        private string HttpGet(string url)
        {
            try
            {
                var request = System.Net.WebRequest.Create(url);
                var response = request.GetResponse();
                var dataStream = response.GetResponseStream();
                var reader = new System.IO.StreamReader(dataStream);
                var stringResponse = reader.ReadToEnd();
                reader.Close();
                response.Close();

                return stringResponse;
            } catch (Exception e)
            {
                return "Error occured: " + e.Message;
            }
        }

        private void HandleResponseInMainThread(string response)
        {
            // this code will be executed on the main thread
            // here you can access to any cAlgo.API members

            if (response.Length <= 20)
                Print(response);
            else
                Print(response.Substring(0, 20) + "...");
        }
    }
}

@Spotware

ClickAlgo
21 Dec 2015, 20:59

Thank you very much Spotware team.


@ClickAlgo