【问题标题】:StackOverflowException was unhandled HResult=-2147023895 [closed]StackOverflowException 未处理 HResult=-2147023895 [关闭]
【发布时间】:2014-08-30 14:24:24
【问题描述】:
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 使 true。 5 分钟后给出 StackOverflowException is unhandled HResult=-2147023895 错误。

感谢您的帮助。

【问题讨论】:

  • 因为您在没有退出条件的情况下递归调用 serialchek()。

标签: c# stack-overflow hresult unhandled httpunhandledexception


【解决方案1】:

您的调用 serialcek() 没有停止就递归调用,从而导致堆栈溢出。

附:使用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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多