StackOverflowException未处理HResult = -2147023895

时间:2014-08-30 14:24:24

标签: c# stack-overflow hresult unhandled httpunhandledexception

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错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

你的呼叫serialcek()递归而不停止,导致堆栈溢出。

P.S。将finallytry\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();
        }
    }