Link to home
Start Free TrialLog in
Avatar of jvalescu
jvalescuFlag for United States of America

asked on

Why wont my button stop this thread?

I have an issue with a real time device monitoring threaded app.  I start the thread via btnStart_Click event and it works fine.  When I click on the "Stop button" ( btnStopCalibration_Click ), the screen updates stop, the device continues to monitor and I get an hourglass.  Using debug, I have set breakpoints at all lines within btnStopCalibration_Click and they never hit.  What's going on?  Thank you!




 private void btnStart_Click(object sender, EventArgs e)
        {
            this.EnvironmentalMonitorThread = new Thread(new ThreadStart(this.Start_Environmental_Monitoring));
            this.EnvironmentalMonitorThread.IsBackground = true;
            this.EnvironmentalMonitorThread.Priority = System.Threading.ThreadPriority.Lowest;
            this.EnvironmentalMonitorThread.Name = "Environmental Monitor";
            this.EnvironmentalMonitorThread.Start();
            this.btnStart.Enabled = false;
            this.tcp2701.connect(tcpip);
            if (this.tcp2701.connected)
                this.tcp2701.config();
            if (this.tcp2701.configok)
                this.Start_Environmental_Monitoring();
           
        }



private void Start_Environmental_Monitoring()
        {
            // Initializes equipment and starts the 2701

 
            string name = "Environment";
            int interval = 10;



            object sender = null;
            System.EventArgs e = null;

            long CurTickValue = Environment.TickCount;
            int counter = 0;

            string placeo = "";
            string strStat = string.Empty;

            this.arTimers.Add(new clsTimeCount());
            ((clsTimeCount)arTimers[0]).TargetTime = 10;
            ((clsTimeCount)arTimers[0]).Active = true;
            ((clsTimeCount)arTimers[0]).Starto();


            while (WorkerThreadAlive)
            {

                if (((clsTimeCount)arTimers[0]).Active)
                {
                    Thread.Sleep(100);
                    CurTickValue = Environment.TickCount;

                     if (this.tcp2701.connected)
                    {
                        this.read();
                           
                    }
                   
       
                    ((clsTimeCount)arTimers[0]).StartTime = Environment.TickCount;

               

                }


            }

 private void read()
        {
         Int16 x;
            string[] rawdata;
            double valstring;
           
     
            this.tcp2701.send("Read?");
            this.tcp2701.KI2701Read(ref tcp2701.KIData);
       
            rawdata = this.tcp2701.KIData.Split(new char[] { ',' });
     
                       this.SetAnyText(this.txtip, rawdata[0]);
         
            this.tcp2701.KIInput(ref tcp2701.KIstatus);
            lblinput.Text = tcp2701.KIstatus;
            buffcounter++;
            if (buffcounter >= 20)
            {
                this.SetLabel(this.lblValue, this.txtip.Text, Color.Black, true);
                buffcounter = 0;
            }
            this.Refresh();

           
     
        }

        }


 private void btnStopCalibration_Click(object sender, EventArgs e)
        {
            WorkerThreadAlive = false;
            this.ExposureWorkerThreadAlive = false;
            this.ElapsedTimeWorkerThreadAlive = false;
            try
            {
                this.ElapsedTimeMonitorThread.Suspend();
                this.ExposureMonitorThread.Suspend();
                this.EnvironmentalMonitorThread.Suspend();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
         //  this.StopWorkerThread();
            this.tcp2701.disconnect();



        }
Avatar of Die-Tech
Die-Tech

Is any other control on your form responsive after you start monitoring?  I'm guessing from the last conditional statement in the event method for the start button that you're running the method again in the UI thread as shown in this code:

if (this.tcp2701.configok)
     this.Start_Environmental_Monitoring();

You designed that method to be called by the Thread that you spawned.  When you call Start on the thread that spawns the thread and calls the method.  But, then you call it again and it sits in an endless loop.
Avatar of jvalescu

ASKER

What would be the best way to change this?  Put the thread init code into the Start_Environmental_Monitoring method and have the btnStart_Click method just call it?  I need to be able to start and stop this process whenever those buttons are pressed.   Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Die-Tech
Die-Tech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial