Yet Another Windows Forms Issue

So I have been playing around with windows forms for a little while now. But I have ran into something rather strange.
When I try to print the i value to the text box it does not seem to update. I am a little bit dumb struck as to why this is. I must ask is there anyway to fix this and why is this happening.
1
2
3
4
5
6
7
8
9
10
11
// Full code if you need it http://pastebin.com/rYYKZ5Lg
private: System::Void goButton_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		int i(0);
		while(true)
		{
		i++;
		theTextBox->Text = Convert::ToString(i);
		mf->Refresh();
		}
	}
A gui is event driven. I.e. there is an event loop that processes/dispatches the messages. goButton_Click(...) was called due to an event. mf->Refresh(); places an event in the event queue.

When you have an infinite loop within an event, the event loop stucks. No event will be processed nor dispatched. Your gui freezes.

If you want some continuous output you need a timer event for each change (you shouldn't need to call Refresh()):

https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.90%29.aspx
Could you provide a clear example?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	private: System::Void goButton_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		time = gcnew System::Timers::Timer(1000);

		time->Elapsed += gcnew ElapsedEventHandler(goF);
		time->Interval = 2000;
		time->Enabled = true;
		/*
		while(true)
		{
		i++;
		theTextBox->Text = Convert::ToString(i);
		mf->Refresh();
		}
		*/
	}
Topic archived. No new replies allowed.