Reputation: 555
Ok hi, I am making a program in Microsoft Visual Studio and every time I run it and and click start (I have a start button), it will do what I have it programmed to do, but the form always freezes and doesn't display what i want it too (it says "Not Responding" once i start it). It is good for doing it job, but I have things on the form that are supposed to be shown. While it keeps freezing, it does not give me the option to stop it, or show any labels I have set to change, during it's running. Any help on this will be appreciated. Thank You.
EDIT: This is what I have:
void CheckAll()
{
for (; ; )
{
CheckPixel();
Application.DoEvents();
}
}
It is constantly doing CheckPixel();, I take it that is the reason why it is freezing. There are never any breaks.
Upvotes: 0
Views: 5182
Reputation: 75386
Your application is freezing because it's in an infinite loop. I don't know how you can fix it, because I don't know exactly what you're trying to do here.
Update: since I need to go to bed, I'm going to toss out a total guess here. Your CheckPixel() method should probably have a signature like this:
public bool CheckPixel(int x, int y)
{
Color color = _myBitmap.GetPixel(x, y);
return (color == Color.Red);
}
where _myBitmap
is a form-scoped Bitmap. Then your CheckAll() method should be something like this:
public bool CheckAll()
{
for (int x = 0; x < _myBitmap.Width; x++)
{
for (int y = 0; y < _myBitmap.Height; y++)
{
if (CheckPixel(x, y))
{
return true;
}
}
}
return false;
}
G'night folks! I'll be here all week.
Upvotes: 4
Reputation: 73594
You need to have some way of exiting out of the loop.
Either your for loop needs the logic to go from ? to ? (as in
for(int i = 0; , < 100; 1++)
which will will loop 100 times
OR
for(;;)
{
if(SomeCondition == true)
{
break;
}
}
Upvotes: 0
Reputation: 76
Instead of Application.DoEvents() why don't you replace with Threading.Thread.Sleep(0)? I'm not an expert but I prefer Thread.Sleep better then DoEvents.
Upvotes: 1
Reputation: 24177
This usually means you are blocking the UI thread (e.g. running a long operation inside a button click handler). Instead of using the UI thread, you will generally need to offload long I/O operations to the ThreadPool or your own worker threads. This is not always easy to do and requires careful design and a good understanding of concurrency, etc.
Upvotes: 4
Reputation: 5925
Put your program in a try-catch block and then have any exception thrown print in a messagebox. http://msdn.microsoft.com/en-us/library/0yd65esw%28VS.80%29.aspx
Also, try inserting a breakpoint at the point of click to identify where exactly it freezes up.
Upvotes: 1