TDM
TDM

Reputation: 83

winform doesn't apear when code is run

i'm building a quick and dirty program to basically turn on a light in my room from a website as a code kata. during the programming i decided i would temporarily use a winform to test instead of hooking the physical light up (and having all sorts of possible problems there). but when i run my program the winform doesn't show, I've tried to run the executable but still nothing. when debugging i can see all the code works fine it's just that the winform doesn't show up. here is all the code:

form1.cs:

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace alarm_light_test
{
    public partial class Form1 : Form
    {

        WebClient client = new WebClient();

        public Form1()
        {
            InitializeComponent();

            while (true)
            {
                if (CheckData())
                {
                    TurnSirenOn();
                    client.DownloadString("**link to .php file to reset the .txt file**");
                    Thread.Sleep(5000);
                    TurnSirenOff();
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }

    public bool CheckData()
        {
            bool retval = false;
            Stream stream = client.OpenRead("**link to online .txt file**");
            StreamReader reader = new StreamReader(stream);
            String content = reader.ReadToEnd();
            if(content == "1")
            {
                retval = true;
            }

            return retval;
        }

        public void TurnSirenOn()
        {
            pictureBox1.BackColor = Color.Green;
        }

        public void TurnSirenOff()
        {
            pictureBox1.BackColor = Color.Red;
        }
    }
}

program.cs:

using System;
using System.Windows.Forms;

namespace alarm_light_test
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs

namespace alarm_light_test
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(13, 13);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(235, 235);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(260, 260);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
    }
}

Upvotes: 0

Views: 1019

Answers (1)

JayV
JayV

Reputation: 3271

In your constructor public Form1(), you have a loop for-ever (a.k.a an infinite loop).

The while(true) will prevent the constructor from finishing the construction of the form object, and therefore will not be able to display anything.

public Form1()
{
    InitializeComponent();

    while (true) // This loop will never exit, and will run forever
    {
        ...
    }
}

Edit: Sample of how to run your code Asynchronously, therefore allowing the form to fully initialise and display as expected.

public Form1()
{
    InitializeComponent();

    DoWorkAsynchronously();
}

private async Task DoWorkAsynchronously()
{
    await Task.Run(() =>
    {
        while (true)
        {
            if (CheckData())
            {
                TurnSirenOn();
                client.DownloadString("**link to .php file to reset the .txt file**");
                Thread.Sleep(5000);
                TurnSirenOff();
            }
            else
            {
                Thread.Sleep(1000);
            }
        }
    });
}

Upvotes: 6

Related Questions