• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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();



        }
0
jvalescu
Asked:
jvalescu
  • 2
1 Solution
 
Die-TechCommented:
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.
0
 
jvalescuAuthor 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.
0
 
Die-TechCommented:
It's hard to say overall, because all I see is a slice of your code.  But, it appears to me that instead of calling Start_Environmental_Monitoring if configok = true that you should move the call to the Start method of the thread there.

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.btnStart.Enabled = false;

            this.tcp2701.connect(tcpip);
            if (this.tcp2701.connected)
                this.tcp2701.config();
            if (this.tcp2701.configok)
                this.EnvironmentalMonitorThread.Start();
        }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now