C# - Displaying a progressbar while executing an SQL Query.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yoavyoavyoav
    New Member
    • Oct 2007
    • 23

    C# - Displaying a progressbar while executing an SQL Query.

    Hi ,

    I would like a progress bar to be displayed while running an sql query. the progress bar doesn't have to move according to the query pace - I just want it to move back and forth while the query is executing , and to disappear when the query is completed.

    My problem is that the progress bar does not appear when the query is running (I'm using the data adapter fill method in order to run my queries). What can I do ?

    Thanks in advance.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You just want something like "Loading... " (or "Saving..." ) or something?

    Create a label, and tell it to be "on top" of the other controls. Then set it to invisible.
    Before you run your query, set it to "visible" and do a like .Update() on the control (to make sure it updates your UI before you start the query)
    Then you can run your query.
    When it's done, just set the label object back to invisible

    Comment

    • yoavyoavyoav
      New Member
      • Oct 2007
      • 23

      #3
      Thanks. Actually the call to the "Update" method was what I was missing. why is it required by the way ?

      Thanks again.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I don't have an authoritative answer to it. But, just that the "normal" GUI calls of updating don't happen when your thread is busy doing something (like executing a big sql statement). So by using the .Update() you force a "one last redraw" to happen before it becomes tied up doing other stuff.

        Comment

        • yoavyoavyoav
          New Member
          • Oct 2007
          • 23

          #5
          Hi ,

          Using the Update did make it appear -but during the query execution it remains static. how can I cause it to change while the query is running (I just want it to move according to my set of instruction - for eg. back and forth). ?

          Thanks.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Make it happen in it's own thread.

            Comment

            • yoavyoavyoav
              New Member
              • Oct 2007
              • 23

              #7
              I didn't find direct instructions on how to use threading (never used it before).
              All I want is to open a new thread in which the progressbar will move back and forth (I have the logic for that so that's not the issue) - and when the query is completed I want the progressbar to disappear.

              I've read about Invoke , etc etc , but didn't find something that worked well.
              What should I do ? thx.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                If you have a function that handles the moving of the "loading... " stuff, when you submit your query just create a new thread and tell it to run that function. Then when your query returns, kill that thread and set the loading screen to invisible again.

                Comment

                • yoavyoavyoav
                  New Member
                  • Oct 2007
                  • 23

                  #9
                  OK...so I think that doesn't really work.
                  I've decided to put my query running in another thread - and I want the progressbar to run while the query is running. How do I know when the query has stopped running if it's in another thread (meaning, is there some sort of a "wait for thread to finish" function ? )

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    myThread.Join() will block the current thread until the other thread completes.
                    Where mythread is the Thread object for your other thread.

                    Comment

                    • iqbalali
                      New Member
                      • Jun 2010
                      • 1

                      #11
                      am doing the same thing but am stuck with the threads join?? will you help about it?

                      Comment

                      • broger
                        New Member
                        • Jul 2010
                        • 2

                        #12
                        re: C# - Displaying a progressbar while executing an SQL Query.

                        You don't really need to join back up with the main thread, nor even wait for the spawned thread to finish. First, you'll need these methods:

                        Code:
                        private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
                                public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
                                {
                                    if (control.InvokeRequired)
                                    {
                                        control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe),
                                            new object[] { control, propertyName, propertyValue });
                                    }
                                    else
                                    {
                                        control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
                                    }
                                }
                                private void StartProgressBar(ProgressBar pb)
                                {
                                    SetControlPropertyThreadSafe(pb, "Visible", true);
                                    SetControlPropertyThreadSafe(pb, "Style", ProgressBarStyle.Marquee);
                                    SetControlPropertyThreadSafe(pb, "MarqueeAnimationSpeed", 100);
                                }
                                private void StopProgressBar(ProgressBar pb)
                                {
                                    SetControlPropertyThreadSafe(pb, "Style", ProgressBarStyle.Blocks);
                                    SetControlPropertyThreadSafe(pb, "MarqueeAnimationSpeed", 0);
                                    SetControlPropertyThreadSafe(pb, "Visible", false);
                                }
                        Using this, you can set the progress bar to either be running or stopped from within your thread. Use these two calls within the threaded method to start/stop:

                        StartProgressBa r(ProgressBar);
                        StopProgressBar (ProgressBar);

                        Generally, between those two, I do some processing, and then call Invoke(new MethodInvoker(< Method>)) to save the results of the thread to the main form. Essentially, it works by a) starting the thread, b) gathering data/perform some sort of task, c) invoking the method which uses the data that you've saved from your thread (which is threadsafe, due to Invoke()), d) finishing it up by stopping the progress bar

                        Hope this helps
                        Last edited by Niheel; Jul 21 '10, 06:45 PM. Reason: Added code tags for code

                        Comment

                        • Sfreak
                          New Member
                          • Mar 2010
                          • 64

                          #13
                          Try using
                          Code:
                          Application.DoEvents();
                          to free your UI while you run those threads.

                          Comment

                          Working...