DEV Community


Why does my winforms app close after a few seconds of loading?

baenencalin profile image Calin Baenen ・1 min read

All I do is start the window.

My code:

using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System;

partial class Program {
    List<Control> Elements;
    Dictionary<String, Color> ClrSchm = ColorScheme.LIGHTS_OUT;
    readonly Form Window;
    String Page = "";

    * Make a new program object.
    private Program() {
        this.Elements = new List<Control>();

        this.Window = new Form();

        this.Window.ShowInTaskbar = true;
        this.Window.StartPosition = FormStartPosition.CenterScreen;
        this.Window.MinimumSize = new Size(500, 400);
        this.Window.Text = $"{Program.NAME} ({Program.VERSION})";

        this.Window.BackColor = this.ClrSchm["background"];
        this.Window.ForeColor = this.ClrSchm["foreground"];
        this.Window.Visible = true;

        this.Elements = Program.PAGES["."];

    * Main method.
    static void Main() {
        Program p = new Program();
        try {
        } catch(Exception EXC) {

    * Run method.
    void Run() {
Enter fullscreen mode Exit fullscreen mode


* Program class.
partial class Program {
    private static readonly Dictionary<String, List<Control>> PAGES =
        new Dictionary<String, List<Control>>() {
            ["."] = {
                Program.ProduceButton(10, 10, text:"Settings"),
                Program.ProduceButton(10, 76, text:"New Program")
Enter fullscreen mode Exit fullscreen mode

The other part of this class isn't important, as I've commented out the call to the method UpdateElements. If it is important, let me know.

So, what am I doing wrong? Why does it just load, and then close? Because the program doesn't even leave behind an EXCEPTION_LOG.txt, like Program.WriteLog should be writing.

Discussion (2)

Editor guide
iankloppers profile image
Ian Kloppers • Edited

Hi Calin,

Try adding the [STAThread] attribute to your Main function, like this:

static void Main() {
Enter fullscreen mode Exit fullscreen mode

Hope this helps.


baenencalin profile image
Calin Baenen Author

Unfortunately, this did not help. I tried putting it both above the Main method, and the method that calls Application.Run, but in both cases, it still retains the quality of loading, and "Not responding".