We help IT Professionals succeed at work.

Why wont my button stop this thread?

jvalescu
jvalescu asked
on
279 Views
Last Modified: 2010-04-16
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();



        }
Comment
Watch Question

Commented:
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.

Author

Commented:
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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.