Click here to Skip to main content
15,891,316 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reading XML Pin
Wendelius10-Nov-08 9:13
mentorWendelius10-Nov-08 9:13 
QuestionLog4Net AsyncAppender Pin
fdbpro10-Nov-08 2:18
fdbpro10-Nov-08 2:18 
AnswerRe: Log4Net AsyncAppender Pin
Simon P Stevens10-Nov-08 2:54
Simon P Stevens10-Nov-08 2:54 
GeneralRe: Log4Net AsyncAppender Pin
fdbpro10-Nov-08 19:22
fdbpro10-Nov-08 19:22 
GeneralRe: Log4Net AsyncAppender Pin
Simon P Stevens10-Nov-08 21:44
Simon P Stevens10-Nov-08 21:44 
AnswerRe: Log4Net AsyncAppender Pin
Dave Kreskowiak10-Nov-08 3:51
mveDave Kreskowiak10-Nov-08 3:51 
GeneralRe: Log4Net AsyncAppender Pin
fdbpro10-Nov-08 19:14
fdbpro10-Nov-08 19:14 
GeneralRe: Log4Net AsyncAppender Pin
Simon P Stevens10-Nov-08 22:07
Simon P Stevens10-Nov-08 22:07 
What you are doing here is potentially a bit risky.

What happens when you call ThreadPool.QueueUserWorkItem is that the method is queued on the thread pool and the main thread continues immediately. Then, when a thread in the pool becomes free, it does the requested work.

By default there are no threads in the pool, and every time work is requested one is created, up until a max (which I think is about 25 by default).

What you are doing is adding 10000 requests to the pool very quickly. So 25 threads will be created. You are then left with 9975 jobs left in the thread pool queue. These will wait until the first 25 begin to finish, then the threads will start processing them.

There are three things to be aware of with this approch.

1) When your reach the objTimer.Stop(); line, your thread pool is still processing the tasks. If you killed your app at this point without letting the thread pool finish, not all the log entries would be written.

2) You cannot guarantee that the tasks will be performed in the order you added them to the thread pool. If the order of the log entries is important, this solution is no good. (Although it's a queue, all this means is that the jobs get started in the correct order, it's possible a job could get pre-empted and switched out before it's written the log so a different log entry could get written first)

3) You should note that the logging still takes the same amount of time, just it's being done away from the main thread so you aren't timing it. This means if your main thread is adding log requests to the thread pool faster than the thread pool can process them you risk the queue getting longer and longer and longer and the thread pool not being able to keep up. Eventually, it will get so far behind that the logs actually get processes ages after they were originally added, and potentially even fill up memory if the queue gets too large. (Also, the same amount of CPU time is taken up. If it's got multiple CPUs the work may be done on a different CPU, but if the system is already maxed out all it's CPUs from your main app, this isn't going to improve anything, all that will happen is that the main app threads will have to run slower to allow the thread pool to work)

Ok. So that's the bad news. However, I have an idea that might help you.

Assuming you are happy with processing on the background thread as means for improving 'performance', you could use a producer/consumer thread queue.

The idea is that you have a queue of tasks. You can add new tasks to the end of the queue. A single thread processes tasks from the front of the queue. It has to be just 1 thread to prevent the risk of the tasks order getting swapped. (Similar to the idea of using the thread pool, but restricting it to one thread). Then, you also add a queue size restriction. When the queue reaches it's maximum size, when an attempt is made to add to it, it blocks until the queue reduces. You could set your queue restriction size to a few thousand, so your app would run, adding tasks to the queue really quickly, but if the queue ever got full, it would slow up to allow the queue to be cleared a bit.

This will only really help if your app makes a lot of log requests in a short period of time, then stops for a bit. This way the log requests won't slow it up during the busy period, and they can be processed in the background while the main app idles.

(This is fairly tricky to implement correctly, so do some research and check out existing produce/consumer queue. There are plenty of examples on google. The tricky bit is making sure the queue is locked correctly during enqueue and dequeue operations, and that the consumer thread waits correctly when the queue is empty)

[Edit: Good luck Wink | ;) ]

Simon

GeneralRe: Log4Net AsyncAppender Pin
fdbpro10-Nov-08 23:35
fdbpro10-Nov-08 23:35 
Questionexport to bitmap Pin
vinay_K10-Nov-08 2:07
vinay_K10-Nov-08 2:07 
AnswerRe: export to bitmap Pin
Guffa10-Nov-08 3:25
Guffa10-Nov-08 3:25 
GeneralRe: export to bitmap Pin
vinay_K10-Nov-08 17:35
vinay_K10-Nov-08 17:35 
GeneralRe: export to bitmap Pin
Guffa10-Nov-08 20:28
Guffa10-Nov-08 20:28 
GeneralRe: export to bitmap Pin
vinay_K10-Nov-08 23:41
vinay_K10-Nov-08 23:41 
Questioni want to get the screen position of the desktop Pin
prasadbuddhika10-Nov-08 0:54
prasadbuddhika10-Nov-08 0:54 
AnswerRe: i want to get the screen position of the desktop Pin
Simon P Stevens10-Nov-08 0:59
Simon P Stevens10-Nov-08 0:59 
QuestionWindows Form Pin
Michael Bookatz10-Nov-08 0:35
Michael Bookatz10-Nov-08 0:35 
AnswerRe: Windows Form Pin
Simon P Stevens10-Nov-08 1:17
Simon P Stevens10-Nov-08 1:17 
AnswerRe: Windows Form Pin
Dave Kreskowiak10-Nov-08 3:46
mveDave Kreskowiak10-Nov-08 3:46 
QuestionShared Document Pin
Sperneder Patrick9-Nov-08 22:54
professionalSperneder Patrick9-Nov-08 22:54 
AnswerRe: Shared Document Pin
Simon P Stevens9-Nov-08 23:42
Simon P Stevens9-Nov-08 23:42 
AnswerRe: Shared Document Pin
Brij10-Nov-08 0:04
mentorBrij10-Nov-08 0:04 
QuestionDistinct LINQ questions Pin
Programm3r9-Nov-08 22:49
Programm3r9-Nov-08 22:49 
AnswerRe: Distinct LINQ questions Pin
Guffa9-Nov-08 23:27
Guffa9-Nov-08 23:27 
GeneralRe: Distinct LINQ questions Pin
Programm3r10-Nov-08 0:00
Programm3r10-Nov-08 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.