[英]C# - Application freezing suddenly
我實際上正在制作游戲,問題是當卡片移動時(我正在使用計時器),它會讓我凍結整個表單,有時它會設法加載,有時它會凍結整個游戲。 (+ 防止單擊 label 並單擊消息框)
問題:(.png)
代碼:(計時器)
public void moveCardsAdv6_2_Tick(object sender, EventArgs e)
{
if (Partie)
{
Distrib = true;
int y6_2 = 48;
int x6_2 = 13;
pictureBoxd6_2.Top += y6_2;
pictureBoxd6_2.Left += x6_2;
#region Adversaire 6
if (pictureBoxd6_2.Top >= 560 && pictureBoxd6_2.Left >= 110)
{
dist6_1 = true;
SecondDistr = true;
moveCardsAdv6_2.Stop();
pictureBoxd6_2.Location = pictureBoxDeck.Location;
GenCartesAdv();
dist6_1 = false;
SecondDistr = false;
moveCardsAdv7_2.Enabled = true;
moveCardsAdv7_2.Start();
}
#endregion
}
}
卡生成代碼
#region Adversaire 6
int CarteAleaA6 = CarteAleatoire();
Cartes carteA6 = jeu[CarteAleaA6];
CartesUtilisees.Add(CarteAleaA6);
int CarteAleaA6_2 = CarteAleatoire();
while (CartesUtilisees.Contains(CarteAleaA6_2))
{
CarteAleaA6_2 = CarteAleatoire();
}
CarteAleaA6_2 = 1 * CarteAleaA6_2;
ListeCartes.Add(carteA6);
if (Distrib && dist6_1)
{
if (SecondDistr == false)
{
pictureBoxAdv6_1.ImageLocation = carteA6.Image;
}
else
{
Cartes carteA6_2 = jeu[CarteAleaA6_2];
CartesUtilisees.Add(CarteAleaA6_2);
ListeCartes.Add(carteA6_2);
pictureBoxAdv6_2.ImageLocation = carteA6_2.Image;
}
}
#endregion
正如其他人所建議的那樣,也許嘗試將它們移動到異步函數中並等待它們。
public async void moveCardsAdv6_2_Tick(object sender, EventArgs e)
{
await moveCardsAdv6_2_TickAsync();
}
private async Task moveCardsAdv6_2_TickAsync()
{
if (Partie)
{
Distrib = true;
int y6_2 = 48;
int x6_2 = 13;
pictureBoxd6_2.Top += y6_2;
pictureBoxd6_2.Left += x6_2;
#region Adversaire 6
if (pictureBoxd6_2.Top >= 560 && pictureBoxd6_2.Left >= 110)
{
dist6_1 = true;
SecondDistr = true;
moveCardsAdv6_2.Stop();
pictureBoxd6_2.Location = pictureBoxDeck.Location;
GenCartesAdv();
dist6_1 = false;
SecondDistr = false;
moveCardsAdv7_2.Enabled = true;
moveCardsAdv7_2.Start();
}
#endregion
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.