0

is there anyway to sleep the some particularthread by name or some any other source in case I've two threads

            Thread call = new Thread(() => open_page(txtbx_1.Text));
            call.Start();

            Thread call_2nd = new Thread(() => open_page(txtbx_1.Text));
            call_2nd.Start();

I want to sleep call and call_2nd for at least 15 minutes(but don't want to sleep the main Thread.

Thanks

Mubin
  • 15
  • 7
  • possible duplicate of [How to enumerate threads in .NET using the Name property?](http://stackoverflow.com/questions/427485/how-to-enumerate-threads-in-net-using-the-name-property) – Eric J. May 01 '14 at 19:01
  • No... thread names need not be unique, and they are not accessible through "normal" (non-debugger level) code. – Eric J. May 01 '14 at 19:02
  • use Thread.Sleep(30) Suspends the current thread for the specified number of milliseconds. – GANI May 01 '14 at 19:03
  • @EricJ.: The question isn't about asking for thread names. This definitely *isn't* a duplicate of the other question. – Jon Skeet May 01 '14 at 19:04
  • You can distinguish between threads by calling [ManagedThreadId](http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx) – 500 - Internal Server Error May 01 '14 at 19:04
  • @JonSkeet: Agreed, though you can't even *find* another thread by name. Your answer about thread cooperation is on target for the real need behind this question. – Eric J. May 01 '14 at 19:05

3 Answers3

3

No - you can't ask another thread to sleep. Apart from anything else, at the time when you want it to sleep it may hold a lock or other resource which really shouldn't be held while sleeping.

You'd have to do this cooperatively, with some sort of shared data between the threads indicating what you want to sleep and for how long. Alternatively, schedule a timer to only start the relevant activity after a certain length of time.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Yes, but in a different way than you might think.

Have the target thread(s) you want to sleep regularly check an manual-reset event. That event (flag) is cleared by the controlling thread whenever the target thread is supposed to sleep, otherwise it is always set. If you want the target thread(s) to sleep only for a certain amount of time, use that value as the wait timeout.

  • if the event is set (no sleep), the wait is satisfied immediately and execution continues
  • if the event is cleared (sleep), the target thread(s) stops until
    • the timeout (15 minutes = 15 * 60 * 1000 ms) elapsed
    • or the controlling thread sets the event again within that time frame

If you need to check whether or not the target thread(s) is in the waiting state, consider another event/flag/counter, set by the target thread(s), and read by the controlling thread.

JensG
  • 13,148
  • 4
  • 45
  • 55
  • That's funny. The answer who says it can't be done gets accepted. The answer who shows a solution doesn't even get an upvote. SO is strange sometimes. – JensG May 02 '14 at 07:43
0

Why would you want a thread to sleep unless it was otherwise going to do something you didn't want it to do? And why would you ever code a thread to do something you didn't want to do?

If you're asking this question, something is very wrong somewhere in your design or implementation.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278