运行代码时,winform不会出现

时间:2018-05-31 15:09:30

标签: c# .net winforms

我正在建立一个快速而肮脏的程序,基本上从我的房间打开一个灯光作为代码kata。在编程期间,我决定暂时使用winform进行测试,而不是将物理灯点亮(并在那里遇到各种可能的问题)。但是当我运行我的程序winform没有显示时,我试图运行可执行文件但仍然没有。调试时我可以看到所有代码都运行正常,只是winform没有出现。这是所有代码:

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;
    }
}

1 个答案:

答案 0 :(得分:6)

在你的构造函数public Form1()中,你有一个永远的循环(a.k.a是一个无限循环)。

while(true)将阻止构造函数完成表单对象的构造,因此无法显示任何内容。

public Form1()
{
    InitializeComponent();

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

编辑:如何异步运行代码的示例,因此允许表单完全初始化并按预期显示。

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);
            }
        }
    });
}