Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i click on button code executes correct but takes some time and " Not Responding Message" appears at form title bar
Posted
Updated 21-May-21 13:22pm

Your application is single threaded. While it is running it's task, there is no thread to keep responding to windows messages. If you perform your task in another thread, then you won't see that message while it is running. A cheaper option is to add the line Application.DoEvents() inside whatever loops your app is running, which will cause it to process messages each time it gets there.
 
Share this answer
 
namespace WindowsFormsApp3{

public partial class Form1 : Form
{

bool allowClick = false;
PictureBox firstGuess;
Random rnd = new Random();
Timer clickTimer = new Timer();
int time = 60;
Timer timer = new Timer { Interval = 1000 };



public Form1()
{ InitializeComponent(); }

private PictureBox[] pictureBoxes
{
get { return Controls.OfType<picturebox>().ToArray(); }
}

private static IEnumerable images
{
get
{
return new Image[]
{
Properties.Resources.Circle,
Properties.Resources.Kite,
Properties.Resources.Rectangle,
Properties.Resources.Rhombus,
Properties.Resources.square,
Properties.Resources.Triangle,
};
}
}

private void startGameTimer()
{
timer.Start();
timer.Tick += delegate
{
time--;
if (time < 0)
{
timer.Stop();
MessageBox.Show("Out of time!");
ResetImages();
}

var ssTime = TimeSpan.FromSeconds(time);

label1.Text = "00: " + time.ToString();
};
}


private void ResetImages()
{
foreach (var pic in pictureBoxes)
{
pic.Tag = null;
pic.Visible = true;
}

HideImages();
setRandomImages();
time = 60;
timer.Start();
}


private void HideImages()
{
foreach (var pic in pictureBoxes)

{
pic.Image = Properties.Resources.Question_Mark;
}
}


private PictureBox getFreeSlot()
{
int num;

do

{
num = rnd.Next(0, pictureBoxes.Count());
}
while (pictureBoxes[num].Tag != null);
return pictureBoxes[num];
}


private void setRandomImages()
{
foreach (var image in images)

{
getFreeSlot().Tag = image;
getFreeSlot().Tag = image;

}
}


private void CLICKTIMER_TICK(object sender, EventArgs e)
{

HideImages();

allowClick = true;
clickTimer.Stop();
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void clickImage(object sender, EventArgs e)
{
if (!allowClick) return;

var pic = (PictureBox)sender;

if (firstGuess == null)
{
firstGuess = pic;
pic.Image = (Image)pic.Tag;
return;
}

pic.Image = (Image)pic.Tag;

if (pic.Image == firstGuess.Image && pic != firstGuess)

{
pic.Visible = firstGuess.Visible = false;
{
firstGuess = pic;

}
HideImages();
}
else
{
allowClick = false;
clickTimer.Start();
}

firstGuess = null;
if (pictureBoxes.Any(p => p.Visible)) return;
MessageBox.Show("You win now try again");
ResetImages();
}

private void startGame(object sender, EventArgs e)
{
allowClick = true;
setRandomImages();
HideImages();
startGameTimer();
clickTimer.Interval = 1000;
clickTimer.Tick += CLICKTIMER_TICK;
button1.Enabled = false;

}


}
}
 
Share this answer
 
Comments
Richard Deeming 24-May-21 6:28am    
Your unformatted, unrelated, and unexplained code-dump is not a "solution" to this already-solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900