Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to write on a textarea when going thru a big loop?

How to write on a textarea when going thru a big loop?

Scheduled Pinned Locked Moved C#
questiontutorial
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eddymvp
    wrote on last edited by
    #1

    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. }

    C B S 3 Replies Last reply
    0
    • E Eddymvp

      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. }

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • E Eddymvp

        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. }

        B Offline
        B Offline
        Bassam Saoud
        wrote on last edited by
        #3

        You need to assign the loop code to a thread...

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          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

          E Offline
          E Offline
          Eddymvp
          wrote on last edited by
          #4

          Thanks alot, I didn't know there was a refresh method. I solved the problem by using this.Refresh();

          G 1 Reply Last reply
          0
          • E Eddymvp

            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. }

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            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

            www.troschuetz.de

            1 Reply Last reply
            0
            • E Eddymvp

              Thanks alot, I didn't know there was a refresh method. I solved the problem by using this.Refresh();

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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;

              E 1 Reply Last reply
              0
              • G Guffa

                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;

                E Offline
                E Offline
                Eddymvp
                wrote on last edited by
                #7

                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?

                E 1 Reply Last reply
                0
                • E Eddymvp

                  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?

                  E Offline
                  E Offline
                  Ed Poore
                  wrote on last edited by
                  #8

                  Application.DoEvents

                  E W 2 Replies Last reply
                  0
                  • E Ed Poore

                    Application.DoEvents

                    E Offline
                    E Offline
                    Eddymvp
                    wrote on last edited by
                    #9

                    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.

                    E 1 Reply Last reply
                    0
                    • E Eddymvp

                      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.

                      E Offline
                      E Offline
                      Ed Poore
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • E Ed Poore

                        Application.DoEvents

                        W Offline
                        W Offline
                        Wayne Phipps
                        wrote on last edited by
                        #11

                        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

                        E 1 Reply Last reply
                        0
                        • W Wayne Phipps

                          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

                          E Offline
                          E Offline
                          Ed Poore
                          wrote on last edited by
                          #12

                          I know, I wouldn't use it but couldn't find the method so I pointed him in the right direction.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups