When the shape is restored, the elements are loaded at the end.



  • There are two forms, one of which is a form filled with one painting (Form3), the second (Form1) - the mein. When applied to the button in Form1, the mein shall be soaked as to open F3:

    Form3 formButton = new Form3();
    formButton.Owner = this;
    formButton.Show();
    formButton.Opacity = 0;
    while (this.Opacity != 0)
    {
        Thread.Sleep(30);
        this.Opacity -= 0.1;
    }
    

    while (formButton.Opacity != 1)
    {
    Thread.Sleep(30);
    formButton.Opacity += 0.1;
    }

    The problem is, when the cycle arrives, there's nothing in shape other than a piece hole. The elements themselves are downloaded only when the programme leaves the cycle.

    Here's the reversal of the mein:

    formMain.Opacity = 0;
    formMain.Show();
    while (formMain.Opacity != 1)
    {
    Thread.Sleep(30);
    formMain.Opacity += 0.1;
    }
    while (this.Opacity != 0)
    {
    Thread.Sleep(30);
    this.Opacity -= 0.1;
    }

    Maybe someone has a simple or not very simple solution to this problem. I would like the entire interface to be smooth, not just shape, and end instantly.



  • As already stated in the commentaries, cycles while c Thread.Sleep inside work in the GUI flow, which leads to the freezing of shape.

    Let's do it with a timer.

    Timers on the dot have a few things different, we're gonna need the one in the name space. System.Windows.Forms.Timer - he can work directly with the controllers, his event. Tick It's the same flow.

    Add two fields to your basic form class:

    System.Windows.Forms.Timer timer;
    Form3 formButton;
    

    And the processor of the timer event:

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (Opacity > 0)
            this.Opacity -= 0.1;
        else if (formButton.Opacity < 1)
            formButton.Opacity += 0.1;
        else
        {
            timer.Stop();
            timer.Tick -= Timer_Tick;
        }
    }
    

    Now your code will look as follows:

    formButton = new Form3();
    formButton.Owner = this;
    formButton.Show();
    formButton.Opacity = 0;
    

    timer = new System.Windows.Forms.Timer();
    timer.Interval = 30;
    timer.Tick += Timer_Tick;
    timer.Start();

    Please note that the properties Opacity shape double♪ So I replaced the exact comparisons. != 0 and != 1> 0 and < 1 - it has to do with the specificity of the presentation of the substance numbers - the exact comparison may not work.

    Also draw attention to the need to list the timer from the event: timer.Tick -= Timer_Tick; - it's necessary to avoid memory leaks.

    If you wish, a timer can be created in a designer of shape, giving it the necessary properties and events.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2