Does not show the form when compiling

1

I have this code below. It is only a Form and a Label . You have to alter the colors over time of% co_of% seconds, in this example I put% co_of% ms to see the change. As long as 0.05 is white, 500 has to be black, then change backwards, that is, the form of white is black and the letters of Form of black to white so for each certain weather. Never show Label on screen.

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

namespace Apruebalos_a_todos_cs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            while (true)
            {
                cambioColor();
                Thread.Sleep(500);
                cambioColor2();
                Thread.Sleep(500);
            }
        }

        void cambioColor()
        {
            this.BackColor = Color.Black; // Formulario negro.
            //this.ForeColor = Color.White; // Cambia textos en blanco.
            label1.ForeColor = Color.White;
        }

        void cambioColor2()
        {
            this.BackColor = Color.White; // Formulario blanco.
            //this.ForeColor = Color.Black; // Cambia textos en negro.
            label1.ForeColor = Color.Black;
        }
    }
}

What am I doing wrong?

Greetings.

    
asked by Meta 08.06.2018 в 04:43
source

2 answers

0

No. He will never show it to you.

The drawing events are in the same thread as the code execution events in your case.

As you put all the code in the load, in an infinite loop, which has a lot of sleeps, what is happening is that the load is executed eternally, and it never gets to draw the form.

To avoid this, your color change function should be in another method apart from the thread, be controlled by a timer (not by a sleep) and, if possible, be an asyncronic function (a call with await), so that the form can respond to the changes that are requested.

To make it work, try the following code.
Add a timer object to your form, and set its interval to 500. Also, assign the tick event to the event that you will find below, add a button so you can cut the program:

public partial class Form1 : Form
{
    private bool state = true;
    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }

    private async void timer1_Tick(object sender, EventArgs e)
    {
        bool b = await CambiarColor();
    }

    private async Task<bool> CambiarColor()
    {
        state = !state;
        if (state)
        {
            this.BackColor = Color.Black; // Formulario negro.
            label1.ForeColor = Color.White;
        }
        else
        {
            this.BackColor = Color.White; 
            label1.ForeColor = Color.Black;
        }
        return true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
    
answered by 08.06.2018 в 07:22
0

Thank you very much.

Before answering, I've done it with two timers. I knew that with one you can also but I have no idea to do it. My code is this.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace _
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        void cambioColor()
        {
            this.BackColor = Color.Black; // Formulario negro.
            //this.ForeColor = Color.White; // Cambia textos en blanco.
            label1.ForeColor = Color.White;
        }

        void cambioColor2()
        {
            this.BackColor = Color.White; // Formulario blanco.
            //this.ForeColor = Color.Black; // Cambia textos en negro.
            label1.ForeColor = Color.Black;
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            cambioColor();
            timer1.Stop();
            timer2.Start();
        }

        private void timer2_Tick(object sender, System.EventArgs e)
        {
            cambioColor2();
            timer2.Stop();
            timer1.Start();
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Since executing the executable, it starts to change background and letter color, but it should not have a button to stop ever.

Thank you.

    
answered by 09.06.2018 в 09:44