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. What is the Best one ? Thread.Sleep() OR Timer

What is the Best one ? Thread.Sleep() OR Timer

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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.
  • C Offline
    C Offline
    Charith Jayasundara
    wrote on last edited by
    #1

    Hi, I am running something on a loop till a certain condition meets. I have two options, 1. Create a timer and execute the loop. 2. This is already running in a child thread so I can just add a while (condition) loop with Thread.Sleep() I prefer to use the second one. But I am not sure which one is the best.. Any Idea? Thanks!

    Charith Jayasundara

    C N 2 Replies Last reply
    0
    • C Charith Jayasundara

      Hi, I am running something on a loop till a certain condition meets. I have two options, 1. Create a timer and execute the loop. 2. This is already running in a child thread so I can just add a while (condition) loop with Thread.Sleep() I prefer to use the second one. But I am not sure which one is the best.. Any Idea? Thanks!

      Charith Jayasundara

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A timer is always best, sleep loops are very ugly and poor form IMO.

      Christian Graus Driven to the arms of OSX by Vista.

      1 Reply Last reply
      0
      • C Charith Jayasundara

        Hi, I am running something on a loop till a certain condition meets. I have two options, 1. Create a timer and execute the loop. 2. This is already running in a child thread so I can just add a while (condition) loop with Thread.Sleep() I prefer to use the second one. But I am not sure which one is the best.. Any Idea? Thanks!

        Charith Jayasundara

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Charith Jayasundara wrote:

        I am running something on a loop till a certain condition meets

        Charith Jayasundara wrote:

        his is already running in a child thread so I can just add a while (condition) loop with Thread.Sleep()

        :confused: Thread.Sleep blocks the current thread for specified time. If your thread slept, how can you check whether the condition met or not? Your question is not clear.

        Navaneeth How to use google | Ask smart questions

        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