By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

Thread Priorities on async operations

P: n/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
Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


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

Gabriel Lozano-Morán
Nov 22 '05 #2

P: n/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.

"Gabriel Lozano-Morán" <ga************@no-spam-thx.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
You could pass the priority as a parameter when calling the methods
asynchronously and then setting the priority.

Gabriel Lozano-Morán

Nov 22 '05 #3

P: n/a
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

Nov 22 '05 #4

P: n/a
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
Nov 22 '05 #5

P: n/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

"Gabriel Lozano-Morán" <ga************@no-spam-thx.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Dec 4 '05 #6

P: n/a
Hi Dave,

Is it really necessary to set the thread priority to below normal?
If yes, I think it would be better you create your own thread and set
its priority instead of changing a pooling thread's priority.
See
http://msdn.microsoft.com/library/en...eadPooling.asp.

Thi

Dec 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.