Very new to C++ playing with CLR

I have played with basic for years but want to exercise the ol gray matter.
As I read thru the books and on line tutorials, I try new things.
Learning to use loops in this case a for loop I wrote a function to count to 10 and display the result in a textbox. Problem is nothing prints until the count is over. If I put in a messagebox the count works fine..

what is wrong?
void countUp(void)
{
int Count;
for (Count=0; Count<10;Count ++)
{
label1->Text = "Count = " + Convert::ToString(Count);
//MessageBox::Show("Count = " + Count);
Sleep (500);
}
}

Thanks
Good reason to start with console i/o in C or C++. Using that you would see your ten lines of output. Your code is writing the string to the label so fast you can't see each individual iteration. All you see I expect is the last one.

The MessageBox() call on the other hand is a blocking synchronous call. It halts the program's execution at every call until you dismiss it.
Last edited on
Gee, I know its fast but why doesn't the Sleep of 500 ms slow it down as does the messagebox stops it?
Should not the label print then we wait half a second then next Count.

My question is how do I fix it.

Thanks
I'm making a guess here. I don't use the .NET CLR. But a similiar situation would ensue in the Win32 Api coding I do. The issue is somewhat complex, but I'll try to explain it. Windows is a message driven operating system. Your code above will run in response to some message. The updating of labels, text boxes, the main application window, etc., occurs in response to other messages - each of which is actually a function call as is your code above. The actual message is WM_PAINT. Well, here's where the plot thickens. When the system is running your loop above, it isn't running/processing WM_PAINT messages, because code execution is stuck in the processing of your code. Putting sleep calls in there simply relinquishes processor time slices to other processes. Its not accomplishing what you think. Every call within that loop to update the label control is causing a WM_PAINT message to be sent to the label control asking it to redraw itself, and if it could, it would. But it can't. Code execution is stuck in your function. When your function finally relinquishes control back to the operating system, all the update messages to the label will occur at once, and you'll only see the end result. Actually, its even a bit more complex than that, but I won't get into it.

To fix the problem you would need to start a 2nd thread of execution which could run simultaenously with your main thread.
Huh, well thanks very much for the input, I've been using VBasic for years and it would seem that it was not a very fertile training ground for C++ I will return to my books.
I think I'm right about what I just told you damorsoft, but I was hoping someone else knowledgible in such things might jump in to confirm or refute what I just wrote.

I used to do VB way back in the 90s before .NET. If you did what you described above in VB would it have worked the way you hoped, i.e., would you have been able to read each number as it went through the loop?

C++ and VB are rather radically different. Some, but not too much will transfer over. An understanding of loops, branching statements (if, select case, etc., will) and so on is pretty common between the two.

The .NET CLR thing with C++ is kind of a hybrid I expect. I was actually interested in it myself a few years back, but never persued it. Too busy with other stuff I guess.
You know I am going to switch back to vb and try it.. weird just one of those little things I dream up..
Been doing lots of reading and it seems CLR is not really being supported anymore in the newer C++ so I am moving on to MFC yikes!!!! Oh well this is what I do stop my brain from drying up...

Thanks for your input, I will be back......
MFC is rather old too. The top choices for doing GUI C++ now are the cross platform toolkits, i.e., QT, wxWidgets, or Win32 Api SDK. The latter is what I do. I've written more info on the latter here...

http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0

The cross platform stuff allows you to relatively easily get your code to run on other platforms such as Linux, phones, etc.

If you are interested in C++ I'd highly recommend starting with console mode programs though for awhile at least, to get some of the basic syntax down.
I have a question for you Damorsoft. I've never seen this style of syntax before.

void countUp(void)

Why are you passing a void as a parameter? Just curious honestly :)
You aren't really "passing" void as a parameter, you're just telling your compiler that function takes no parameters. It's exactly the same as
void countUp()

Last edited on
closed account (z05DSL3A)
damorsoft, Try:
1
2
3
4
5
6
7
8
9
10
void countUp(void)
{
    int Count;
    for (Count=0; Count<10;Count ++)
    {
        label1->Text = "Count = " + Convert::ToString(Count);
        label1->Refresh();
        Sleep (500);
    }
}
Thanks mutexe!
Last edited on
Topic archived. No new replies allowed.