Exception error using windows forms in a thread

Created at 02 May 2019, 04:16
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!
lec0456's avatar

lec0456

Joined 14.11.2012

Exception error using windows forms in a thread
02 May 2019, 04:16


I have a candle countdown Bot that uses windows forms.  I has twice given me an error.

I pretty much got the code from a thread on this forum.  Is there any reason you can think of that this would occur?  See code below.

using cAlgo.API;
using System.Threading;
using System.Windows.Forms;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    
    public class myCandleCountDown : Robot
    {
        [Parameter("Alert On", DefaultValue = true)]
        public bool paramAlertOn { get; set; }
        [Parameter("Always On Top", DefaultValue = true)]
        public bool paramOnTop { get; set; }

        private Thread _thread;
        private frmCandleCountdown _counter;
        readonly string alertFile = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\cAlgo\Sources\Robots\CountdownTimer.wav";
        
        protected override void OnStart()
        {
            Timer.Start(1);
            _counter = new frmCandleCountdown(this, paramOnTop);
            _thread = new Thread(() =>{ _counter.ShowDialog(); });
            _thread = new Thread(() => { Application.Run(_counter); });
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();    
        }

        protected override void OnTimer()
        {
            int cdMinutes = 14 - Time.Minute % 15;
            int cdSeconds = 59 - Time.Second;
            _thread = new Thread(() =>{ _counter.UpdateCounter(cdMinutes.ToString("00")+":"+cdSeconds.ToString("00"));});
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
            if (cdMinutes==1 && cdSeconds==0 && paramAlertOn)
            {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer(alertFile);
                player.Play();
            }
        }
         
        protected override void OnStop()
        {
            _thread = new Thread(() => {_counter.Close();});
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
    } 
} 

 


@lec0456
Replies

firemyst
02 May 2019, 05:54

Out of curiosity, have you tried using Visual Studio to debug line by line to see which line (if any) it occurs on?


@firemyst

lec0456
02 May 2019, 08:21

I can use Visual Studio.  But it runs just fine.  It is only after running for a long period of time that it crashes. Maybe the thread is bleeding memory?? I don't know I am not an expert on threaded procedures.i was capable enough to find most of the code on this Forum and modify it for my application.


@lec0456

PanagiotisCharalampous
06 May 2019, 12:03

Hi lec0456,

What is this line of code supposed to do?

            _thread = new Thread(() => { Application.Run(_counter); });

Seems redundant to me.

Best Regards,

Panagiotis


@PanagiotisCharalampous

lec0456
06 May 2019, 12:13

Ok, so I started using that line instead of the _count.ShowDialog because the show dialog would open on any screen, not in the center of the application that called it.  And i think I forgot to comment out the Show dialog after using it. So yes that looks redundant.

Thanks. 


@lec0456