How to write on a textarea when going thru a big loop?
-
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop. I tried putting a Thread.Sleep timeout of 5 seconds and it still doesn't display the text. How can I get it to display the text before it enters to the loop.
messageRTB.Text= "Running \r\n"; messageRTB.Text += " Please Wait, this will take a while..."; Thread.Sleep(5000); // Num is unknown and it can be greater than 100. for (int i=1; i<=Num; i++) { // Do Something. }
-
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop. I tried putting a Thread.Sleep timeout of 5 seconds and it still doesn't display the text. How can I get it to display the text before it enters to the loop.
messageRTB.Text= "Running \r\n"; messageRTB.Text += " Please Wait, this will take a while..."; Thread.Sleep(5000); // Num is unknown and it can be greater than 100. for (int i=1; i<=Num; i++) { // Do Something. }
Eddymvp wrote:
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop.
You are not allowing the user interface a chance to update the display. It only does this once it has finished processing your request. The request doesn't finish until it goes through the loop. There is a way to get it to refresh the display, but I can't remember how off the top of my head.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop. I tried putting a Thread.Sleep timeout of 5 seconds and it still doesn't display the text. How can I get it to display the text before it enters to the loop.
messageRTB.Text= "Running \r\n"; messageRTB.Text += " Please Wait, this will take a while..."; Thread.Sleep(5000); // Num is unknown and it can be greater than 100. for (int i=1; i<=Num; i++) { // Do Something. }
You need to assign the loop code to a thread...
-
Eddymvp wrote:
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop.
You are not allowing the user interface a chance to update the display. It only does this once it has finished processing your request. The request doesn't finish until it goes through the loop. There is a way to get it to refresh the display, but I can't remember how off the top of my head.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
I have a program that I want to write on a textarea before it goes thru the the loop, and for some reason it just hands there and write to the text area after it finished going thru the loop. I tried putting a Thread.Sleep timeout of 5 seconds and it still doesn't display the text. How can I get it to display the text before it enters to the loop.
messageRTB.Text= "Running \r\n"; messageRTB.Text += " Please Wait, this will take a while..."; Thread.Sleep(5000); // Num is unknown and it can be greater than 100. for (int i=1; i<=Num; i++) { // Do Something. }
Calling the
Refresh
method should resolve your problem:messageRTB.Text += " Please Wait, this will take a while...";
messageRTB.Refresh();
Thread.Sleep(5000);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Thanks alot, I didn't know there was a refresh method. I solved the problem by using this.Refresh();
Calling the Refresh method does update the display, but the window remains unresponsive. If you want it to handle events (like moving the window, or redrawing it in case the user switched to another window while waiting), you should call the DoEvents method instead.
--- single minded; short sighted; long gone;
-
Calling the Refresh method does update the display, but the window remains unresponsive. If you want it to handle events (like moving the window, or redrawing it in case the user switched to another window while waiting), you should call the DoEvents method instead.
--- single minded; short sighted; long gone;
-
Thanks for the reply Guffa, I didn't think that was possible to do. I tried going to this.DoEvent and it wasn't on the list, can you give me an example on how I would call that method?
-
Thanks for the reply, I'm making more progress, now that I can move the windows and the proccess is going too long. The Application doesn't allow me to click any buttons until it executes that loop, Which other methods do I have to call to be able to perform that request.
-
Thanks for the reply, I'm making more progress, now that I can move the windows and the proccess is going too long. The Application doesn't allow me to click any buttons until it executes that loop, Which other methods do I have to call to be able to perform that request.
The best bet would be to create a thread to process the loop in the background, if you're using .NET 2 then there's a BackgroundWorker class which takes away most of the headaches of creating and managing your own threads. Basically you have an event which when called you do your work (this occurs in a different thread) and there are methods of returning progress data in a thread safe manner to the form to update. This should result in now hangs and the user can continue using the program as normal.
-
DoEvents should be avoided. Please read the following: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassDoEventsTopic.asp If you code is using loop or doing anything which is locking up the gui, it would be best to move the offending code to a new thread. Just my opinion
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
-
DoEvents should be avoided. Please read the following: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassDoEventsTopic.asp If you code is using loop or doing anything which is locking up the gui, it would be best to move the offending code to a new thread. Just my opinion
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog