tags:

views:

446

answers:

5

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.

+1  A: 

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.

stormist
+4  A: 

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.

bobbymcr
+1 - good link - a good step in the right direction for handling this type of thing properly.
David Stratton
Also look at BackgroundWorker. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Ray
Perhaps it's better to learn how for loops work before tackling multithreading?
MusiGenesis
+4  A: 

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.

MusiGenesis
Hey, where did all these monkeys come from? And why don't they adhere to the "single exit point" philosophy?
MusiGenesis
Well, okay, maybe that's personal preference, I should've changed it to a more elaborate `break`ing scheme ... well, that would be confusing. Okay. Maybe I should've left it alone. But hey, monkey see ...
Noon Silk
No, your way is better, but we have to take baby steps with someone who puts "for (; ; )" in their code. :)
MusiGenesis
No, that might work but my CheckPixel has something to do with checking that pixels in a game and if those pixels at a certain (x, y), then do a command. That just gave me an idea though, for the bool, I'll test it out and tell you the results.
Brandon
@Brandon: it sounds like this *is* something you want to be constantly checking, so you probably want to use a BackgroundWorker for this as Ray mentioned (and put `Thread.Sleep(0)` or `Thread.Sleep(15)` in your loop instead of `DoEvents()`).
MusiGenesis
+1  A: 

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.

jsoques
Does that allow the UI to respond? How can it respond if it's sleeping? I'll have to try that out. Thank you!
David Stratton
It does not allow the UI to respond AFAIK. Sleep()ing on the main UI thread will just block until the timer is up, not allowing the thread to process anything UI related.
Gregory
Well, I'm not sure, but I think it lets the CPU to do other tasks that can free other process'. I don't think that it will just block.
jsoques
A: 

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;
   }
}
David Stratton