程序里用了backgroundWorker来不停的读取串口数据的操作, /// <summary>
/// 接收各种数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void readAD_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (read)
{
if (comport.IsOpen)
{
Thread.Sleep(10);
int n = comport.BytesToRead;
if (n > 0)
{
byte[] buf = new byte[n]; comport.Read(buf, 0, n);
buffs.AddRange(buf); contin = true;
while (contin && buffs.Count >= 3)
{
deleData(buffs);
}
}
}
}
}
catch (Exception ex) { StatusLabel.Text = ex.Message; }
}
关闭程序的时候 ,我把read 设置为false
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();//使用Save方法保存更改、
read = false;
while (readAD.IsBusy) { }
}
但是这里判断这个backgroundworker,一直都没关闭,程序就死在那里了!!也许你会说,doWork里面的 while (contin && buffs.Count >= 3)
{
deleData(buffs);
}
可能一直,没退出,但是,可以肯定的是,现在完全没进入这个里面的while
为啥关闭不了!!!!
疑惑求解
/// 接收各种数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void readAD_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (read)
{
if (comport.IsOpen)
{
Thread.Sleep(10);
int n = comport.BytesToRead;
if (n > 0)
{
byte[] buf = new byte[n]; comport.Read(buf, 0, n);
buffs.AddRange(buf); contin = true;
while (contin && buffs.Count >= 3)
{
deleData(buffs);
}
}
}
}
}
catch (Exception ex) { StatusLabel.Text = ex.Message; }
}
关闭程序的时候 ,我把read 设置为false
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();//使用Save方法保存更改、
read = false;
while (readAD.IsBusy) { }
}
但是这里判断这个backgroundworker,一直都没关闭,程序就死在那里了!!也许你会说,doWork里面的 while (contin && buffs.Count >= 3)
{
deleData(buffs);
}
可能一直,没退出,但是,可以肯定的是,现在完全没进入这个里面的while
为啥关闭不了!!!!
疑惑求解
执行了啊,调试的时候 就发现read = false;
while (readAD.IsBusy) { }
这里的while一直没有判断到 readAD.isBusy = false,然后程序就死在那里了
else
break;
{
if (comport.IsOpen)
{
Thread.Sleep(10);
int n = comport.BytesToRead;
if (n > 0)
{
byte[] buf = new byte[n]; comport.Read(buf, 0, n);
buffs.AddRange(buf); contin = true;
while (contin && buffs.Count >= 3)
{
deleData(buffs);
}
}
else
break;
}
else
break;
}
你用过while... else???我break了,还怎么不停的读取,我只是要在关闭窗口的时候 才让它停下来!