Thread.yeild

  • Thread starter Thread starter Benjamin The Donkey
  • Start date Start date
B

Benjamin The Donkey

Is there something in C# that is similar to Java's Thread.yield?
Currently I am using the 1.1 framework.
 
Is there something in C# that is similar to Java's Thread.yield?
Currently I am using the 1.1 framework.

It's been a while since I've worked with Java, but if I remember
correctly, it is giving up the current threads timeslice... If that's
the case:

Thread.Sleep(0) should do the trick.
 
Tom said:
It's been a while since I've worked with Java, but if I remember
correctly, it is giving up the current threads timeslice... If that's
the case:

Thread.Sleep(0) should do the trick.

You will want to call with a parameter of 1 instead of 0 if you want to
guarantee that the call will yield to some other thread (in particular,
a lower-priority thread).

Calling with 0 for the parameter will only allow another thread to run
if that thread is of equal or higher priority to the thread making the
call. If there's no such thread, the thread making the call will
continue to run, even if there are lower-priority threads ready and
waiting to run.

If that's not a concern, Thread.Sleep(0) should work fine.

Pete
 
You will want to call with a parameter of 1 instead of 0 if you want to
guarantee that the call will yield to some other thread (in particular,
a lower-priority thread).

Calling with 0 for the parameter will only allow another thread to run
if that thread is of equal or higher priority to the thread making the
call. If there's no such thread, the thread making the call will
continue to run, even if there are lower-priority threads ready and
waiting to run.

If that's not a concern, Thread.Sleep(0) should work fine.

Pete

You know, I just recently read that... You learn something new
everyday.
 
You will want to call with a parameter of 1 instead of 0 if you want to
guarantee that the call will yield to some other thread (in particular,
a lower-priority thread).

Calling with 0 for the parameter will only allow another thread to run
if that thread is of equal or higher priority to the thread making the
call. If there's no such thread, the thread making the call will
continue to run, even if there are lower-priority threads ready and
waiting to run.

If that's not a concern, Thread.Sleep(0) should work fine.

Pete

hmm... i am not sure if it makes sense to yeild to a thread with lower
priority anyway?
 
Benjamin said:
hmm... i am not sure if it makes sense to yeild to a thread with lower
priority anyway?

It would depend on why you're yielding in the first place. But yes,
there are situations in which you would want to call Sleep(0) instead of
Sleep(1). It just depends on what behavior you want.

My point in replying was to make sure it's understood that Sleep(0) does
not guarantee that your thread will actually yield to some other thread.
If that's okay, or even desirable, then Sleep(0) is indeed what you'd
want to do.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

build APi 2
Framework 1.1 - opening Excel file 1
Hibernate analog for .NET 9
C# WinForm on Framwork1.1 and Framwork3.5 1
Class structure 10
32-bit .net 1.1 apps on Win2003 x64 8
C# v2.0 4
csc.exe is missing 3

Back
Top