开发者

.NET: Will tracelistener run on a seperate low priority thread?

开发者 https://www.devze.com 2022-12-21 23:06 出处:网络
I was provided by an tracing API which开发者_运维知识库 was developed in house which directly writes to database on the same thread of the caller. I didnt like the approach. I thought an implementing

I was provided by an tracing API which开发者_运维知识库 was developed in house which directly writes to database on the same thread of the caller. I didnt like the approach. I thought an implementing custom database tracelistner but wondering if it will run on a seperate low priority thread or do I have manage threading myself.


First, you shouldn't try and set the priority of a thread. Generally speaking, it's a bad idea, because thread priority elevation (or deelevation in this case) will be overriden by the OS in the event that it feels those threads are being starved (in the case of de-prioritizing the thread) or taking up too much time (in the case of elevation).

That being said, in .NET, it's very easy to make database calls aynchronously. For example, the SqlCommand class has a number of methods that use the Begin* pattern for asynchronous calls, and most other database providers in .NET follow suit.

That being said, if your trace API allows for it, you should use one of these mechanisms to start the database operation, and then finish up in a callback you provide to the async mechanism.

If you are not using a provider that allows for asynchronous calls, then you should probably use the ThreadPool class (specifically a call to QueueUserWorkItem) to queue an asynchronous operation (this assumes the trace operation is not lenghty).

If you are using .NET 4.0, then you might even want to take a look at the classes in the System.Threading.Tasks namespace to do ThreadPool-like operations (there is a better API which allows for multiple pools, cancellation, and other features the ThreadPool doesn't have).

It should be noted that you should get all the relevant information you need to perform the operation before you start it. For example, you want to get the date/time of the operation before you make the asynchronous call, as you want that value to be reflective of the operation you are tracing, not the trace itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号