Threading Task with a message

Created at 25 Sep 2020, 15:32
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!
CT

ctid2568413

Joined 14.09.2020

Threading Task with a message
25 Sep 2020, 15:32


Hello,

I display the form asynchronously with threading task. It works when it is written this way:

 

public class Indicator : Indicator
{
//button, label, form declared

public override void Calculate(int index)
{
if (something)
ShowForm();
}

ShowForm()
{
//button, label, form properties and the controls added

var asyncTask = new System.Threading.Tasks.Task(() => { form.ShowDialog(); });
asyncTask.Start();
}

}

 


@ctid2568413
Replies

ctid2568413
28 Sep 2020, 05:31

When I want to start the task on an API's button click on the chart, the form is either missing its controls, disappearing quickly or crashing cTrader.

 

//apiButton declared and the properties are set

apiButton.Click += ButtonClickEventArgs =>
{
var asyncTask = new System.Threading.Tasks.Task(() => { form.ShowDialog(); });
asyncTask.Start();
};

 

Please advise.


@ctid2568413

PanagiotisCharalampous
28 Sep 2020, 08:48

Hi ctid2568413,

We need the complete cBot code in order to provide further assistance.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

ctid2568413
29 Sep 2020, 05:00

Hi, this is an example of how the main structure is written. Is the logic of the task and its synchronization with the API button correct.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using Forms = System.Windows.Forms;
using Drawing = System.Drawing;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class asitest : Indicator
    {
        [Output("Main", LineColor = "#FFFFFFFF")]
        public IndicatorDataSeries asiResult { get; set; }

        AccumulativeSwingIndex asi;

        DateTime lastAlert;

        Button apiButton = new Button();

        Forms.Button button = new Forms.Button();
        Forms.Label label = new Forms.Label();
        Forms.Form form = new Forms.Form();

        protected override void Initialize()
        {
            asi = Indicators.AccumulativeSwingIndex(12);

            lastAlert = Bars.Last(1).OpenTime;

            ApiButton();
        }

        public override void Calculate(int index)
        {
            asiResult[index] = asi.Result[index];

            if (!IsLastBar)
                return;

            var isAbove = asi.Result.Last(1) <= 0 && asi.Result.Last(0) > 0;

            if (isAbove)
            {
                var subjectBuy = "Is Above";
                var bodyBuy = "Bullish";

                Notify("buy", subjectBuy, bodyBuy);
            }

            var isBelow = asi.Result.Last(1) >= 0 && asi.Result.Last(0) < 0;

            if (isBelow)
            {
                var subjectSell = "Is Below";
                var bodySell = "Bearish";

                Notify("sell", subjectSell, bodySell);
            }
        }

        private void Notify(string type, string subject, string body)
        {
            if (Bars.LastBar.OpenTime == lastAlert)
                return;

            lastAlert = Bars.LastBar.OpenTime;

            ShowForm(type, subject, body);
        }

        private void ShowForm(string type, string subject, string body)
        {
            Drawing.Color aColor = Drawing.Color.Yellow;

            switch (type)
            {
                case "buy":
                    aColor = Drawing.Color.Green;
                    break;
                case "sell":
                    aColor = Drawing.Color.Red;
                    break;
            }

            button.Location = new Drawing.Point(35, 50);
            button.ForeColor = Drawing.Color.White;
            button.BackColor = aColor;
            button.Text = "OK";
            button.DialogResult = Forms.DialogResult.OK;

            label.Location = new Drawing.Point(35, 25);
            label.Height = 75;
            label.Width = 75;
            label.ForeColor = Drawing.Color.White;
            label.BackColor = aColor;
            label.Text = body;
            label.TextAlign = Drawing.ContentAlignment.TopLeft;
            label.Font = new System.Drawing.Font("Arial", 10);

            form.StartPosition = Forms.FormStartPosition.CenterScreen;
            form.Height = 150;
            form.Width = 150;
            form.TopMost = true;
            form.BringToFront();
            form.FormBorderStyle = Forms.FormBorderStyle.FixedDialog;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.BackColor = aColor;
            form.Text = subject;
            form.AcceptButton = button;
            form.KeyPreview = true;

            form.Controls.Add(button);
            form.Controls.Add(label);

            apiButton.IsEnabled = true;
            apiButton.IsVisible = true;

            apiButton.Click += ButtonClickEventArgs =>
            {
                apiButton.IsEnabled = false;
                apiButton.IsVisible = false;

                var asyncShowForm = new System.Threading.Tasks.Task(() => { form.ShowDialog(); });
                asyncShowForm.Start();
            };
        }

        private void ApiButton()
        {
            apiButton.ForegroundColor = Color.White;
            apiButton.BackgroundColor = Color.Yellow;
            apiButton.Height = 100;
            apiButton.Width = 100;
            apiButton.HorizontalAlignment = HorizontalAlignment.Center;
            apiButton.VerticalAlignment = VerticalAlignment.Center;
            apiButton.HorizontalContentAlignment = HorizontalAlignment.Center;
            apiButton.VerticalContentAlignment = VerticalAlignment.Center;
            apiButton.IsEnabled = false;
            apiButton.IsVisible = false;

            IndicatorArea.AddControl(apiButton);
        }
    }
}

 


@ctid2568413

PanagiotisCharalampous
29 Sep 2020, 08:25 ( Updated at: 21 Dec 2023, 09:22 )

Hi ctid2568413,

I tried the indicator and worked fine for me

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

ctid2568413
02 Oct 2020, 05:54 ( Updated at: 21 Dec 2023, 09:22 )

Hi and thank you for your response.

If you let the indicator go for a few minutes with multiple symbols at one minute time frame, you will notice that it shows some forms correctly and some are either missing contents, the button, are freezing and impossible to be closed or even crashing cTrader. I haven't noticed that when there was no API's button and the forms were being shown directly.

I suspect the logic might need improvement. I am involved in several job projects based on the logic and would be very thankful for your support.

 


@ctid2568413

ctid2568413
08 Oct 2020, 05:05

I still hope for an assistance of the Spotware team.


@ctid2568413

PanagiotisCharalampous
08 Oct 2020, 08:12

Hi ctid2568413,

I could not reproduce the issue unfortunately. Also this is not a cTrader related issue but a general WinForms programming issue. You may want to think of a different approach for your application.

Best Regards,

 

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous