实例化一个类会导致堆栈溢出 [英] Instantiaing a class causes stack overflow

查看:304
本文介绍了实例化一个类会导致堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个解决方案,允许线程在后台处理数据时更新WinForm属性(TextBox)。我已经整理了一个示例项目,它在主程序运行时启动一个线程来更新WinForm。



这是主要的表单类:



I am trying to build a solution that will allow a thread to update WinForm properties (TextBox) while processing data in the background. I've put together a "sample" project that starts a thread to update the WinForm while the main program runs.

Here is the main Form Class:

namespace Thread_Test_Sending_Data_to_Thread_Function
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            int i;
            InitializeComponent();
            FormData FormData1 = new FormData();
            Thread1 MyThread = new Thread1();

 
            for (i=0; i<10; i++)
            {
                FormData1.Label1Data = i;
                FormData1.Label1Data = i * 10;

                Thread WriteScreen = new Thread(delegate ()
                {
                    MyThread.FormWriteMethod(FormData1);
                });

                WriteScreen.Start();
                Thread.Sleep(1000);
            }
           
        }



这里是线程类




and here is the thread class

namespace Thread_Test_Sending_Data_to_Thread_Function
{
    public class Thread1 : Form1
    {
        private delegate void SetLabelTextDelegate(FormData InputClass);
        public void FormWriteMethod(FormData InputClass)
        {
            if (TestLabel1.InvokeRequired)
            {
                TestLabel1.BeginInvoke(new SetLabelTextDelegate(FormWriteMethod), new object[] { InputClass });
                return;
            }
            TestLabel1.Text = InputClass.Label1Data.ToString();
            TestLabel2.Text = InputClass.Label2Data.ToString();
        }
    }
}



一切都建立好。我相信我已经设置了这一点,允许我向线程发送FormData类的引用。 FormData由主窗体程序加载,然后导致线程被执行。它暂停线程完成,然后更新FormData。 Thread继承了Form1,因此它应该可以访问Form1中定义的TestLable1 TextBox



当我尝试运行程序时程序进入语句Thread1 MyThread = new线();然后循环回程序的开头,直到发生堆栈溢出。我一定很遗憾。有什么建议吗?



我的尝试:



我是看了很多关于如何向线程发送数据以及如何从线程访问WinForm数据的示例。根据我的发现,我所做的似乎是正确的。不知道下一步该尝试什么。


Everything builds OK. I believe I've set this up to allow me to send a reference to the FormData class to the thread. FormData is loaded by the main form program which then causes the thread to be executed. It pauses for the thread to complete and then updates the FormData. The Thread has inherited Form1 so it should have access to the TestLable1 TextBox defined in Form1

When I try to run the program the program gets to the statement Thread1 MyThread = new Thread(); and then loops back to the beginning of the program until a stack overflow occurs. I must be missing something simple. Any suggestions?

What I have tried:

I've looked at numerous examples of how to send data to a thread and how to access the WinForm data from the thread. From what I have found, what I am doing seems to be correct. Not sure what to try next.

推荐答案

简单:

Simple:
public Form1()
{
    int i;
    InitializeComponent();
    FormData FormData1 = new FormData();
    Thread1 MyThread = new Thread1();






And

public class Thread1 : Form1



因为Thread1是从Form1派生的,所以在构造一个新的Thread1实例时,会调用Form1构造函数,它会创建一个Thread1实例,该实例调用它自己的Form1构造函数,该构造函数创建为Thread1实例,该实例调用.. 。



在某些时候系统耗尽堆栈内存来保存它的所有返回地址,你的应用程序崩溃。


Because Thread1 is derived from Form1, when you construct a new Thread1 instance, the Form1 constructor is called, which creates a Thread1 instance, which calls it's own Form1 constructor, which creates as Thread1 instance, which calls ...

At some point the system runs out of stack memory to hold all it's return addresses and your app crashes.


那里这里有很多不妥之处我不知道从哪里开始。



但是,我会告诉你它会抛出堆栈溢出因为Form1正在创建一个Thread1的实例。 Thread1继承自Form1,它创建了一个Thread1实例,它继承自Form1,它创建了一个Thread1实例,它继承自Form1,它创建了一个Thread1实例,它继承自Form1,它创建了一个Thread1实例,...得到这个想法了吗?这就是运行堆栈的原因。


我不知道为什么你有从Thread1继承Thread1的原因。更新标签的代表和方法应该在Form1上。



你在Form1上运行的工作应该在其他地方。我不知道,比如Thread1?



你已经连续两行设置了LabelData1,这根本不会有用。第一个值永远不会在屏幕上绘制,因为UI线程仍然会挂起运行将LabelData1设置为i * 10的代码。



这样做过于复杂所有错误类中的代码。
There are soooo many things wrong in here I don't know where to start.

But, I'll tell you that it throws a stack overflow because Form1 is creating an instance of Thread1. Thread1 inherits from Form1, which creates an instance of Thread1, which inherits from Form1, which creates an instace of Thread1, which inherits from Form1, which creates an instance of Thread1, which inherits from Form1, which creates an instance of Thread1, ... get the idea yet? This is what's running the stack out.

I have no idea why you've got Thread1 inheriting from Form1 at all. The delegate and methods to update the label should be on Form1.

The work you've got running on Form1 should be somewhere else. I don't know, like Thread1?

You've got LabelData1 being set by two consecutive lines, which won't do anything useful at all. The first value will never be painted on screen because the UI thread will still be hung up running the code that's setting LabelData1 to i*10.

This is way overcomplicated with code in all the wrong classes.


这里的错误,你将2个值存储在同一个对象中。

Bug here, you store 2 values in same object.
FormData1.Label1Data = i;
FormData1.Label1Data = i * 10;



您也可以删除第一行。



我不知道是什么你记得,但你处于一个循环中,这个值是2个线程彼此区分的唯一方法,但由于它是一个对象并且值不断变化,它打败了给出一个数字的想法每个线程。你在制作与对方比赛的主题吗?


You can as well remove the first line.

I don't know what you have in mind, but you are in a loop and this value is the only way for 2 thread to distinguish from each other, but since it is an object and that the value is continuously changing, it defeat the idea of giving a number to each thread. Are you making threads that race against each others ?


这篇关于实例化一个类会导致堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆