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 use progress bar according to the code execution timing.

How to use progress bar according to the code execution timing.

Scheduled Pinned Locked Moved C#
tutorial
12 Posts 8 Posters 1 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.
  • S sr159

    I have created one windows application form, in that form loading i written code and for execution it will take 1 or 2 minutes (lenghty code). This i need to show in progress bar so that user can think process is going. I used progress bar in that form in loading. The progress bar is not showing but code is executing. How to use progress bar according to the code execution timing. Please reply me. Thanks in advance.

    C Offline
    C Offline
    coolestCoder
    wrote on last edited by
    #3

    Please use this[^] class. You will find many example when you Google for it.

    Found my answer helpful? Then vote for it.


    "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


    coolestCoder

    1 Reply Last reply
    0
    • S sr159

      I have created one windows application form, in that form loading i written code and for execution it will take 1 or 2 minutes (lenghty code). This i need to show in progress bar so that user can think process is going. I used progress bar in that form in loading. The progress bar is not showing but code is executing. How to use progress bar according to the code execution timing. Please reply me. Thanks in advance.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #4

      In all probability, you are loading the form and displaying the progress bar using the UI thread. What you need to do is separate out the logic that takes a long time to run into a background thread, and periodically refresh/pulse the progress bar. I would argue though, that you have a poor user design if they have to wait 2 minutes for a form to load. I'd drop kick any developer who released code like that for me.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      G 1 Reply Last reply
      0
      • S sr159

        I have created one windows application form, in that form loading i written code and for execution it will take 1 or 2 minutes (lenghty code). This i need to show in progress bar so that user can think process is going. I used progress bar in that form in loading. The progress bar is not showing but code is executing. How to use progress bar according to the code execution timing. Please reply me. Thanks in advance.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #5

        If you want a snappy GUI and/or you want to show some progress, you should limit the execution time of each of your GUI event handlers to say less than 30 msec. So having lengthy operations in a Form Load handler is no good; you should instead: - launch a thread (possibly a BackgroundWorker) in the Form Load handler; - have that thread perform the lengthy operation (e.g. database access) and store results in some data structure; - then use Invoke to have the GUI thread update the GUI, showing the newly acquired data, and probably enabling the Controls that now should be functional. When the operation that needs to show progress is indivisible, you can't really show progress, all you can indicate is that time is progressing, so you should estimate a worst-case time span, and show a progress bar that advances at periodic points in time. A System.Windows.Forms.Timer is excellent for such purpose. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        1 Reply Last reply
        0
        • A Ankur m

          You just had to type "Progress bar C#" into Google search box instead of typing so much here. Check the search result - progress bar c#[^]

          ..Go Green..

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #6

          Actually, it's a good job the user did. What they were displaying is a classic case of attempting to run multiple things on the UI thread. Had they just asked how to show the progress bar, they might not appreciate the difficulties in what they were trying to accomplish.

          I'm not a stalker, I just know things. Oh by the way, you're out of milk.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Actually, it's a good job the user did. What they were displaying is a classic case of attempting to run multiple things on the UI thread. Had they just asked how to show the progress bar, they might not appreciate the difficulties in what they were trying to accomplish.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            A Offline
            A Offline
            Ankur m
            wrote on last edited by
            #7

            I am sorry I missed the main issue in the question. I checked your and Luc's reply below. How do I implement a progress bar in C#?[^] - this is from the very first page of the search link and if I understood your answer correctly, this is what you were talking about below. All I meant to say that the OP should have done a little research before asking.

            ..Go Green..

            1 Reply Last reply
            0
            • P Pete OHanlon

              In all probability, you are loading the form and displaying the progress bar using the UI thread. What you need to do is separate out the logic that takes a long time to run into a background thread, and periodically refresh/pulse the progress bar. I would argue though, that you have a poor user design if they have to wait 2 minutes for a form to load. I'd drop kick any developer who released code like that for me.

              I'm not a stalker, I just know things. Oh by the way, you're out of milk.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              G Offline
              G Offline
              GenJerDan
              wrote on last edited by
              #8

              I take it you've never run Calibre[^]. Takes for_ever_ to load. Have no idea what it's doing, other than not starting promptly. :laugh:

              There is water at the bottom of the ocean. My Mu[sic] My Films My Windows Programs, etc.

              P 1 Reply Last reply
              0
              • G GenJerDan

                I take it you've never run Calibre[^]. Takes for_ever_ to load. Have no idea what it's doing, other than not starting promptly. :laugh:

                There is water at the bottom of the ocean. My Mu[sic] My Films My Windows Programs, etc.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #9

                GenJerDan wrote:

                Have no idea what it's doing

                Thread.Sleep(30000000);

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                G 1 Reply Last reply
                0
                • P Pete OHanlon

                  GenJerDan wrote:

                  Have no idea what it's doing

                  Thread.Sleep(30000000);

                  I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

                  G Offline
                  G Offline
                  GenJerDan
                  wrote on last edited by
                  #10

                  Ah! A function call designed to dissuade all who are not seriously committed.

                  There is water at the bottom of the ocean. My Mu[sic] My Films My Windows Programs, etc.

                  1 Reply Last reply
                  0
                  • S sr159

                    I have created one windows application form, in that form loading i written code and for execution it will take 1 or 2 minutes (lenghty code). This i need to show in progress bar so that user can think process is going. I used progress bar in that form in loading. The progress bar is not showing but code is executing. How to use progress bar according to the code execution timing. Please reply me. Thanks in advance.

                    _ Offline
                    _ Offline
                    _Erik_
                    wrote on last edited by
                    #11

                    Look at this[^]. Yes, I wrote it... :-D

                    1 Reply Last reply
                    0
                    • S sr159

                      I have created one windows application form, in that form loading i written code and for execution it will take 1 or 2 minutes (lenghty code). This i need to show in progress bar so that user can think process is going. I used progress bar in that form in loading. The progress bar is not showing but code is executing. How to use progress bar according to the code execution timing. Please reply me. Thanks in advance.

                      R Offline
                      R Offline
                      RaviRanjanKr
                      wrote on last edited by
                      #12

                      hi sr159 Sometimes there's no practicable way to measure progress towards completion, using a simple mathematical formula. In such cases, you need to estimate in advance how long the operation will take and then change the ProgressBar in accordance with the percentage time elapsed since inception. You also need to avoid moving the progress bar up to 100% until the overall task has completed. take a look there[^]

                      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