public void serialcek()
{
while (!exitThread)
{
foreach (ManagementObject currentObject in theSearcher.Get())
{
try
{
textBox1.Text = currentObject["Size"].ToString() + " " + currentObject["PNPDeviceID"].ToString();
currentObject.Dispose();
}
catch (Exception)
{
// MessageBox.Show("Bişiler oldu bende anlamadım");
currentObject.Dispose();
//exitThread = false;
}
}
}
Thread.Sleep(100);
serialcek();
}
我使用线程。但几分钟后出现错误。单击按钮是exitThread make true。比5分钟后给出一个StackOverflowException未处理HResult = -2147023895错误。
感谢您的帮助。
答案 0 :(得分:2)
你的呼叫serialcek()
递归而不停止,导致堆栈溢出。
P.S。将finally与try\catch
一起使用以防止代码重复:
public void serialcek()
{
while (!exitThread)
{
foreach (ManagementObject currentObject in theSearcher.Get())
{
try
{
textBox1.Text = currentObject["Size"].ToString() + " " + currentObject["PNPDeviceID"].ToString();
}
catch (Exception)
{
// MessageBox.Show("Bişiler oldu bende anlamadım");
//exitThread = false;
}
finally
{
currentObject.Dispose();
}
}
}
Thread.Sleep(100);
if(<condition>) // add your condition here
{
serialcek();
}
}