Thread Priorities on async operations

D

Dave A

I have a low priority background thread doing some async socket
communications.

The thread that kicks off the AsyncRead/AsyncWrite has its priority set to
'below normal'. But when the code enters the AsyncRead/AsyncWrite functions
I have discoved that the priority has been changed to 'normal'. I
understand that new threads in .Net do not inherit their creators priority
(which is cool) but what I can't understand is how to convince the
AyncRead/Write thread to be the priority of its creator.

The only thing I can think of is use some private class member to record the
creators priority when the class is created and then in the
AsyncRead/AsyncWrite to manually change the priority to the value in the
member - seems ugly to me.

/// <summary>
/// Sends the data asynchronously
/// </summary>
/// <param name="ar"></param>
private void SendAsync(IAsyncResult ar)
{
try
{
Thread.CurrentThread.Priority = this.originalThreadPriority;
...

Any suggestions?

Regards
Dave A
 
G

Gabriel Lozano-Morán

You could pass the priority as a parameter when calling the methods
asynchronously and then setting the priority.

Gabriel Lozano-Morán
 
D

Dave A

There is the 'object state' parameter that can be passed into
stream.BeginRead and stream.BeginWrite but I am already using this
parameter. I could create a new class that includes the thread priority but
it seems a pretty awkward solution.

Apparently Java's threads inherit the priority of their parent which would
seems a more sensible solution.
 
T

Truong Hong Thi

Hi Dave,
There is the 'object state' parameter that can be passed into
stream.BeginRead and stream.BeginWrite but I am already using this
parameter. I could create a new class that includes the thread priority but
it seems a pretty awkward solution.
Yes, pretty awkward but I think you may need it. Kind of EventArgs,
right? Think that you might even want to pass something else in the
future. It could be a nested class.
Apparently Java's threads inherit the priority of their parent which would
seems a more sensible solution.
It is true that a .NET thread does not inherit its parent priority.
However, I think .NET makes use of ThreadPool internally for such async
delegates. Members of a pool should be identical.

Regards,
Thi
 
G

Gabriel Lozano-Morán

Anyway if you set the priority to low you have absolutely no guarantee
whatsoever that the priority will not be changed by the operating system.

Gabriel Lozano-Morán
 
D

Dave A

What do you mean?

I did not know that the OS could arbitrarily change the priority of threads.
If it does happen then it must happen under extraordinary circumstances like
an imminent power failure reported by the UPS or something. I have never
seen this happen.

Can you please quote some source.

Its not that I don't believe you - its just that I don't believe you. :)

Regards
Dave A
 

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

Top