jvalescu
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.EnvironmentalMonitorT hread = new Thread(new ThreadStart(this.Start_Env ironmental _Monitorin g));
this.EnvironmentalMonitorT hread.IsBa ckground = true;
this.EnvironmentalMonitorT hread.Prio rity = System.Threading.ThreadPri ority.Lowe st;
this.EnvironmentalMonitorT hread.Name = "Environmental Monitor";
this.EnvironmentalMonitorT hread.Star t();
this.btnStart.Enabled = false;
this.tcp2701.connect(tcpip );
if (this.tcp2701.connected)
this.tcp2701.config();
if (this.tcp2701.configok)
this.Start_Environmental_M onitoring( );
}
private void Start_Environmental_Monito ring()
{
// 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] ).TargetTi me = 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] ).StartTim e = Environment.TickCount;
}
}
private void read()
{
Int16 x;
string[] rawdata;
double valstring;
this.tcp2701.send("Read?") ;
this.tcp2701.KI2701Read(re f 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.lblValu e, this.txtip.Text, Color.Black, true);
buffcounter = 0;
}
this.Refresh();
}
}
private void btnStopCalibration_Click(o bject sender, EventArgs e)
{
WorkerThreadAlive = false;
this.ExposureWorkerThreadA live = false;
this.ElapsedTimeWorkerThre adAlive = false;
try
{
this.ElapsedTimeMonitorThr ead.Suspen d();
this.ExposureMonitorThread .Suspend() ;
this.EnvironmentalMonitorT hread.Susp end();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToStrin g());
}
// this.StopWorkerThread();
this.tcp2701.disconnect();
}
private void btnStart_Click(object sender, EventArgs e)
{
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.btnStart.Enabled = false;
this.tcp2701.connect(tcpip
if (this.tcp2701.connected)
this.tcp2701.config();
if (this.tcp2701.configok)
this.Start_Environmental_M
}
private void Start_Environmental_Monito
{
// 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]
((clsTimeCount)arTimers[0]
((clsTimeCount)arTimers[0]
while (WorkerThreadAlive)
{
if (((clsTimeCount)arTimers[0
{
Thread.Sleep(100);
CurTickValue = Environment.TickCount;
if (this.tcp2701.connected)
{
this.read();
}
((clsTimeCount)arTimers[0]
}
}
private void read()
{
Int16 x;
string[] rawdata;
double valstring;
this.tcp2701.send("Read?")
this.tcp2701.KI2701Read(re
rawdata = this.tcp2701.KIData.Split(
this.SetAnyText(this.txtip
this.tcp2701.KIInput(ref tcp2701.KIstatus);
lblinput.Text = tcp2701.KIstatus;
buffcounter++;
if (buffcounter >= 20)
{
this.SetLabel(this.lblValu
buffcounter = 0;
}
this.Refresh();
}
}
private void btnStopCalibration_Click(o
{
WorkerThreadAlive = false;
this.ExposureWorkerThreadA
this.ElapsedTimeWorkerThre
try
{
this.ElapsedTimeMonitorThr
this.ExposureMonitorThread
this.EnvironmentalMonitorT
}
catch (Exception ex)
{
MessageBox.Show(ex.ToStrin
}
// this.StopWorkerThread();
this.tcp2701.disconnect();
}
ASKER
What would be the best way to change this? Put the thread init code into the Start_Environmental_Monito ring 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (this.tcp2701.configok)
this.Start_Environmental_M
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.