Сбой в async анимации увеличения кнопки

Рейтинг: 0Ответов: 0Опубликовано: 14.01.2023

Столкнулся с такой проблемкой. На форме есть 4 кнопки при наведении которой они увеличиваются в ширину . Так вот я использовал async чтобы плавно её анимировать , вроде получилось как надо , но собственно проблема : При быстром наведении на все кнопки они начинают хаотично увеличиваться в размере не достигая нужного размера

Как выглядит после медленного наведения на все кнопки как выглядит после медленного наведения на все кнопки :

Как выглядит после быстрого наведения на все кнопки как выглядит после быстрого наведения на все кнопки

Код :

private void MainWindow_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            new Thread(NewGameAnimation).Start();
            new Thread(RecordsAnimation).Start();
            new Thread(CreditsAnimation).Start();
            new Thread(ExitAnimation).Start();
        }
        //=========================================================
        //=========================================================
        //=========================================================
        private void NewGameAnimation()
        {
            //=========================================================
            //====================NewGame Animation====================
            //=========================================================
            NewGame.MouseEnter += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    NewGame.Size = new Size(NewGame.Width + NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && NewGame.Width <= 450);
            };

            NewGame.MouseLeave += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && NewGame.Width >= 350);
            };
            NewGame.Click += new EventHandler(GameStart);
        }
        private void RecordsAnimation()
        {
            //=========================================================
            //=================RecordButton Animation==================
            //=========================================================
            Records.MouseEnter += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Records.Size = new Size(Records.Width + Records.Width / 35, Records.Height);
                    Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                    Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                    Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                    Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                    Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                    NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Records.Width <= 450);
            };
            Records.MouseLeave += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                    Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Records.Width >= 350);
            };
        }
        private void CreditsAnimation()
        {
            //=========================================================
            //=================CreditsButton Animation=================
            //=========================================================
            Credits.MouseEnter += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Credits.Size = new Size(Credits.Width + Credits.Width / 35, Credits.Height);
                    Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                    Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                    Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                    Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                    Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                    NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Credits.Width <= 450);
            };
            Credits.MouseLeave += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                    Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Credits.Width >= 350);
            };
        }
        private void ExitAnimation()
        {
            //=========================================================
            //==================ExitButton Animation===================
            //=========================================================

            Exit.MouseEnter += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Exit.Size = new Size(Exit.Width + Exit.Width / 35, Exit.Height);
                    Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                    Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                    Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                    Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                    Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                    NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Exit.Width <= 450);
            };
            Exit.MouseLeave += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                    Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && Exit.Width >= 350);
            };
        }

Ответы

Ответов пока нет.