Exception

Created at 29 Apr 2019, 14:44
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!
UP

upwithhumility

Joined 30.03.2018

Exception
29 Apr 2019, 14:44


Hello,

i need your help cause i can not solve my issue; i try to open a form when starting my robot but get this error :


Crashed in OnStart with ThreadStateException: unable to instanciate ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2', cause actual thread is not a partiation thraed (STA, Single-Threaded Apartment).
 

 

Here is my code :

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Threading;


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

        private Thread _thread;
        private Form _f1;

        [STAThread()]
        protected override void OnStart()
        {
            // Put your initialization logic here
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread = new Thread(() => { Application.Run(_f1); });
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }

    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSendOrders_Click(object sender, EventArgs e)
        {
            var confirmResult = MessageBox.Show("Are you sure to send orders in the Market ?", "Confirm !", MessageBoxButtons.YesNo);

            if (confirmResult == DialogResult.Yes)
            {
                MessageBox.Show("Orders successfully sent in market.", "Info");
            }
            else
            {
                MessageBox.Show("Request aborted.", "Info");
            }
        }

        private void buttonSplit_Click(object sender, EventArgs e)
        {
            webBrowser.Navigate("https://web.telegram.org/");
        }
        /// <summary>
        /// Variable nécessaire au concepteur.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Nettoyage des ressources utilisées.
        /// </summary>
        /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Code généré par le Concepteur Windows Form

        /// <summary>
        /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
        /// le contenu de cette méthode avec l'éditeur de code.
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBoxPosition = new System.Windows.Forms.GroupBox();
            this.buttonSplit = new System.Windows.Forms.Button();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.buttonSendOrders = new System.Windows.Forms.Button();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.labelSL = new System.Windows.Forms.Label();
            this.labelTP3 = new System.Windows.Forms.Label();
            this.labelTP2 = new System.Windows.Forms.Label();
            this.labelTP1 = new System.Windows.Forms.Label();
            this.labelEntry = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.labelOrderType = new System.Windows.Forms.Label();
            this.webBrowser = new System.Windows.Forms.WebBrowser();
            this.groupBoxPosition.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBoxPosition
            // 
            this.groupBoxPosition.Controls.Add(this.buttonSplit);
            this.groupBoxPosition.Controls.Add(this.textBox5);
            this.groupBoxPosition.Controls.Add(this.buttonSendOrders);
            this.groupBoxPosition.Controls.Add(this.textBox4);
            this.groupBoxPosition.Controls.Add(this.textBox3);
            this.groupBoxPosition.Controls.Add(this.textBox2);
            this.groupBoxPosition.Controls.Add(this.textBox1);
            this.groupBoxPosition.Controls.Add(this.labelSL);
            this.groupBoxPosition.Controls.Add(this.labelTP3);
            this.groupBoxPosition.Controls.Add(this.labelTP2);
            this.groupBoxPosition.Controls.Add(this.labelTP1);
            this.groupBoxPosition.Controls.Add(this.labelEntry);
            this.groupBoxPosition.Controls.Add(this.comboBox1);
            this.groupBoxPosition.Controls.Add(this.labelOrderType);
            this.groupBoxPosition.Location = new System.Drawing.Point(22, 12);
            this.groupBoxPosition.Name = "groupBoxPosition";
            this.groupBoxPosition.Size = new System.Drawing.Size(954, 219);
            this.groupBoxPosition.TabIndex = 0;
            this.groupBoxPosition.TabStop = false;
            this.groupBoxPosition.Text = "Position manager";
            // 
            // buttonSplit
            // 
            this.buttonSplit.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
            this.buttonSplit.Location = new System.Drawing.Point(575, 65);
            this.buttonSplit.Name = "buttonSplit";
            this.buttonSplit.Size = new System.Drawing.Size(125, 23);
            this.buttonSplit.TabIndex = 12;
            this.buttonSplit.Text = "Split orders info";
            this.buttonSplit.UseVisualStyleBackColor = false;
            this.buttonSplit.Click += new System.EventHandler(this.buttonSplit_Click);
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(382, 24);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(488, 20);
            this.textBox5.TabIndex = 11;
            // 
            // buttonSendOrders
            // 
            this.buttonSendOrders.BackColor = System.Drawing.Color.LightGreen;
            this.buttonSendOrders.Location = new System.Drawing.Point(80, 186);
            this.buttonSendOrders.Name = "buttonSendOrders";
            this.buttonSendOrders.Size = new System.Drawing.Size(100, 23);
            this.buttonSendOrders.TabIndex = 10;
            this.buttonSendOrders.Text = "Place Orders";
            this.buttonSendOrders.UseVisualStyleBackColor = false;
            this.buttonSendOrders.Click += new System.EventHandler(this.buttonSendOrders_Click);
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(80, 148);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 20);
            this.textBox4.TabIndex = 9;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(80, 113);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 20);
            this.textBox3.TabIndex = 8;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(80, 75);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 7;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(80, 45);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 6;
            // 
            // labelSL
            // 
            this.labelSL.AutoSize = true;
            this.labelSL.Location = new System.Drawing.Point(9, 151);
            this.labelSL.Name = "labelSL";
            this.labelSL.Size = new System.Drawing.Size(26, 13);
            this.labelSL.TabIndex = 5;
            this.labelSL.Text = "SL :";
            // 
            // labelTP3
            // 
            this.labelTP3.AutoSize = true;
            this.labelTP3.Location = new System.Drawing.Point(9, 120);
            this.labelTP3.Name = "labelTP3";
            this.labelTP3.Size = new System.Drawing.Size(36, 13);
            this.labelTP3.TabIndex = 4;
            this.labelTP3.Text = "TP 3 :";
            // 
            // labelTP2
            // 
            this.labelTP2.AutoSize = true;
            this.labelTP2.Location = new System.Drawing.Point(9, 98);
            this.labelTP2.Name = "labelTP2";
            this.labelTP2.Size = new System.Drawing.Size(36, 13);
            this.labelTP2.TabIndex = 3;
            this.labelTP2.Text = "TP 2 :";
            // 
            // labelTP1
            // 
            this.labelTP1.AutoSize = true;
            this.labelTP1.Location = new System.Drawing.Point(8, 75);
            this.labelTP1.Name = "labelTP1";
            this.labelTP1.Size = new System.Drawing.Size(36, 13);
            this.labelTP1.TabIndex = 1;
            this.labelTP1.Text = "TP 1 :";
            // 
            // labelEntry
            // 
            this.labelEntry.AutoSize = true;
            this.labelEntry.Location = new System.Drawing.Point(7, 48);
            this.labelEntry.Name = "labelEntry";
            this.labelEntry.Size = new System.Drawing.Size(37, 13);
            this.labelEntry.TabIndex = 2;
            this.labelEntry.Text = "Entry :";
            // 
            // comboBox1
            // 
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] 
            {
                "Buy",
                "Sell"
            });
            this.comboBox1.Location = new System.Drawing.Point(80, 16);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 21);
            this.comboBox1.TabIndex = 1;
            // 
            // labelOrderType
            // 
            this.labelOrderType.AutoSize = true;
            this.labelOrderType.Location = new System.Drawing.Point(9, 24);
            this.labelOrderType.Name = "labelOrderType";
            this.labelOrderType.Size = new System.Drawing.Size(37, 13);
            this.labelOrderType.TabIndex = 0;
            this.labelOrderType.Text = "Type :";
            // 
            // webBrowser
            // 
            this.webBrowser.Location = new System.Drawing.Point(198, 237);
            this.webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser.Name = "webBrowser";
            this.webBrowser.Size = new System.Drawing.Size(592, 700);
            this.webBrowser.TabIndex = 1;
            this.webBrowser.WebBrowserShortcutsEnabled = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(988, 961);
            this.Controls.Add(this.webBrowser);
            this.Controls.Add(this.groupBoxPosition);
            this.Name = "Form1";
            this.Text = "CMulti TP order";
            this.groupBoxPosition.ResumeLayout(false);
            this.groupBoxPosition.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBoxPosition;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Label labelOrderType;
        private System.Windows.Forms.Label labelTP1;
        private System.Windows.Forms.Label labelEntry;
        private System.Windows.Forms.Label labelSL;
        private System.Windows.Forms.Label labelTP3;
        private System.Windows.Forms.Label labelTP2;
        private System.Windows.Forms.Button buttonSendOrders;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button buttonSplit;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.WebBrowser webBrowser;

    }
}

 

could you help me to solve it please ? 

thanks in advance,

Alexandre


@upwithhumility
Replies

firemyst
02 May 2019, 05:47

RE:

In the OnStart method, you're creating two instances of _thread.

You create one first with the show dialog.

Then immediately on the next line, you create another NEW thread-object, and assign it to _thread. Thus,_thread no longer has the show dialog.


@firemyst