Comment initialiser correctement une application Windows Forms

J'ai écrit cet exemple de programme - simplification d'une très complexe de l'application. Le même binaire [.exe] renvoie une exception de pointeur null sur certaines machines au démarrage. Donc, je veux savoir comment bien construire un Windows Forms forme.

Dans notre application, la Form1_SizeChanged méthode est le résultat de this.ResumeLayout(false) méthode qui est la dernière instruction dans InitializeComponents(). Je ne sais pas à simuler, donc j'ai juste changé la taille moi-même pour ce programme de test.

public partial class Form1 : Form
{
    public class Logger {
        public Logger() { }
        public void log(string str) {
            Console.WriteLine("logging - " + str);
        }
    }

    Logger logger = null;

    public Form1()
    {
        InitializeComponent();
        this.Size = new Size(200, 300);
        MyInitialize();
    }

    private void MyInitialize() {

        //Just that it takes some time.
        Console.WriteLine("MyInitialize -- Enter");
        for (int count = 0; count <5 ; count++){
            Console.WriteLine("Sleeping -- " + count);
            Thread.Sleep(1000);
        }
        logger = new Logger();
    }

    private void sleepingPill(int millisec) {
        Thread.Sleep(millisec);
    }

    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        logger.log("Form1_SizeChanged -- Enter");
    }
}

Selon ma compréhension, Form1_SizeChanged ne devrait pas être appelée à moins que Form1 est bien construit. Quelqu'un peut jeter un peu de lumière sur la façon dont les Windows Forms architecture d'événements de travailler dans ce scénario?


Original Stack Trace: from our complex application
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=ABCD
StackTrace:
at ABCD.Form1.AppendToLog(String s)
at ABCD.Form1.Form1_SizeChanged(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height)
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Form.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.ScaleControl(SizeF factor, BoundsSpecified specified)
at System.Windows.Forms.ScrollableControl.ScaleControl(SizeF factor, BoundsSpecified specified)
at System.Windows.Forms.Form.ScaleControl(SizeF factor, BoundsSpecified specified)
at System.Windows.Forms.Control.ScaleControl(SizeF includedFactor, SizeF excludedFactor, Control requestingControl)
at System.Windows.Forms.ContainerControl.Scale(SizeF includedFactor, SizeF excludedFactor, Control requestingControl)
at System.Windows.Forms.ContainerControl.PerformAutoScale(Boolean includedBounds, Boolean excludedBounds)
at System.Windows.Forms.ContainerControl.PerformNeededAutoScaleOnLayout()
at System.Windows.Forms.ContainerControl.OnLayoutResuming(Boolean performLayout)
at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout)
at ABCD.Form1.InitializeComponent()
at ABCD.Form1..ctor()
at ABCD.Program.Main()
InnerException:

Avis de la trace de la pile: Form1_sizeChanged est appelé à partir de InitializeComponents() .. ce qui je pense ne devrait pas arriver. Form1_sizeChanged est une méthode d'instance de la classe Form1 et ne doit pas être appelée avant la Form1 est construit. Si l' .NET de l'environnement à traiter ce cas, il doit attendre jusqu'à Form1 est bien construit, n'est-ce pas?

  • Où est cette exception? Quelles machines? Avez-vous une trace de la pile?
  • La trace de pile ajoutées à la question.
InformationsquelleAutor Karephul | 2011-02-10