Thread sleep with double

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    Thread sleep with double

    Hello guys,

    I want to wait a certain time but it wont be always integer values, but decimals also.

    For example I want to wait half second and in my program it's used like 0.5.

    Do you have any solution to this?

    Best regards,
    Raul Bolanos.
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    The integer-value passed to Thread.Sleep represents the milliseconds you want the thread to sleep.
    So if you want your Thread to sleep for half a second, pass 500 to it.
    You can also take your double (as a value representing seconds), multiply it with 1000(, round it) and cast it as an integer before passing it to Thread.Sleep()

    Comment

    • raulbolanos
      New Member
      • Apr 2009
      • 31

      #3
      Thank you it's solved now.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Thread.Sleep in any form is evil (in my personal opinion)

        NOTHING happens on the thread during that time... No event detection... no UI update...

        What is you are doing that you want to halt everything for? If you can find a better way, such as a function callback when something completes it would be a lot better. Then you aren't guessing at how much time. If it takes 500mS or 5 full seconds... you can pickup when the function is done.

        Or... Make a timer for the amount of time to wait. Start it. Then continue when it raises a Tick event.

        Comment

        • Christian Binder
          Recognized Expert New Member
          • Jan 2008
          • 218

          #5
          I agree with your opinion, that it is bad to do Sleep() in the GUI-Thread, but it's not written here, that the Sleep()-method is called in the GUI-Thread.
          It some cases (,I think), a Sleep() isn't that bad. E.g. when polling or doing much work, which would affect and slow down other Threads in a single-core-environment.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            But if you are going to poll, then use a timer and do your check on each Tick.
            Let the thread run, but be idle the rest of the time. Rather than sleep.
            Just my personal opinion that it's a bad habit to get in to.
            If you are polling a device for example, and your thread is sleeping then you won't receive the event of Fail or Disconnect or EmergencyStop or whatever that device might need to throw at you.

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              I would also use a Timer (System.Windows .Forms when having a GUI or System.Threadin g when not) to do polling ... (It was just a thought to use Thread.Sleep() in that case)

              What do you me with letting the Thread be rather idle than sleep?
              The scenario I meant by using Sleep() for less affecting other Thread's performance would be like
              Code:
              new System.Threading.Thread(() => ThreadedProcedure()).Start();
                 
              ...
              
              void ThreadedProcedure() {
                while (x) {
                  DoHeavyWorkX();
                  Thread.Sleep(20);
                }
              
                while (y) {
                  DoHeavyWorkY();
                  Thread.Sleep(20);
                }
              }
              I'm sure, there will be better ways, to solve such a problem, but it's easy to do so and when there aren't any exceptions thrown, I think it's not that unsecure.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Sleeping for 20 milliseconds won't even be noticeable.

                The scenario I meant by using Sleep() for less affecting other Thread's performance
                If they are on other threads, then this thread shouldn't affect their performance anyway. That's the point of doing different work on different threads.

                Try commenting out your sleep statements and let me know if you can see any difference in your other threads' performances.

                Comment

                • Christian Binder
                  Recognized Expert New Member
                  • Jan 2008
                  • 218

                  #9
                  I'll post a real scenario in which I took usage of Thread.Sleep()

                  Code:
                  void TEST_Threaded() {
                        System.Diagnostics.Debug.WriteLine("TEST_Threaded called ...");
                  
                        if (_breakThread)
                          return;
                        
                        // This shows a kind of progressbar until the work is done
                        Invoke(new Action(() => ShowPanel(true)));
                  
                        // List for my BeginInvoke-results
                        List<IAsyncResult> ress = new List<IAsyncResult>();
                  
                        foreach (var x in GUIController.SubCategories) {
                          if (_breakThread)
                            return;
                          
                          System.Diagnostics.Debug.WriteLine("Starting ...");
                  
                          // change something within the ui
                          ress.Add(BeginInvoke(new Action(() => x.Value.SetFlowLayoutPanel(new Size(Width - 100, 0)))));
                  
                          // Sleeping ... the GUI-Progressbar would be blocked if this isn't done
                          Thread.Sleep(200);
                        }
                  
                        System.Diagnostics.Debug.WriteLine("Waiting for all to be finished ...");
                  
                        while (ress.Count > 0 && !_breakThread) {
                          // Wait for all IAsyncResults to be finished
                          for (int i = ress.Count - 1; i >= 0; i--) {
                            if (ress[i].IsCompleted) {
                              ress.Remove(ress[i]);
                            }
                          }
                  
                          Thread.Sleep(50);
                        }
                  
                        System.Diagnostics.Debug.WriteLine("All finished ... ");
                  
                        if (_breakThread)
                          return;
                  
                        // Hide the progressbar panel
                        Invoke(new Action(() => ShowPanel(false)));
                      }
                  The Thread.Sleep(20 0); is the part, where I need Thread.Sleep() otherwise my GUI would be blocked. The GUI is a simple panel with a picturebox on it, showing an animated gif like this common AJAX-graphic often used on websites during load of content.
                  I do not know, how to do this without Thread.Sleep() maybe you've got a better idea.

                  Comment

                  Working...