Posts

Showing posts from May, 2013

Process - Thread - Task Nightmare

I found out one more thing today - a process, task and thread can all have different priorities all independent of each other. For a task, you cannot legitimately change its priority without causing issues and it is not recommended, for a thread, you want to reduce priority to prevent it from locking to and holding 100% CPU doing some work. However, the caveat with using threads is that it runs at a lower security context from a thread pool thread a.k.a task - hence, either change its security context internally OR us a task instead in such cases. Yes, even if the process itself runs as a user which has access (to say the IIS), the thread spinned out from it, may not be able to read IIS - we have an issue where the first time the service starts up, it does not have access, although the second time, after we restart it, it does have access. Weird.

Divide and Round to Next

public   static   int  DivideAndRound( int  x,  int  y)         {              return  ( int ) ( Math .Ceiling(( double )(( double )x / ( double )y)));         }

Everything you thought you knew about .NET Threading is wrong.

A few years ago, everybody used to should about XML and only use XML for everything. It was a good case of people rushing to something because of the hype and using it for scenarios it was not meant to be used. I have to say I fell for the TPL library hard. It is actually a piece of dirt which has been over hyped and sold as a solution to all threading issues. Let me make it clear here, the TPL fails miserably because as per the recommendations, we can't set the thread priority without screwing up its internal scheduler, and because we can't do that, if the task contains a for or while loop which loops fast enough the processor will use 100% CPU. That by itself is not a problem. The issue is because the task runs with normal priority, no other process can get access to the processor easily and things slow down considerably. Hence, I have tested and verified that if you know that you are going to use a limited number of threads, and you want to split up work which might ...

A class for parallelizing any For Loop in C#

From: foreach (Entity item in collection) {       DoWork( item); } To: LoopParallelizer .Execute< Entity >( TaskCreationOptions .PreferFairness,                                             collection,                                            new Action<Entity>( DoWork )); This will basically split any for loop into a configurable number of threads and run these threads in parallel and wait till all of them are over. The bigger the collection, the faster will be the performance gain by doing t...

How to get a usable hash code of an empty file for comparison purposes

This puzzled me for a while, but I have a solution now. Just create a hash from the file name because file contents are empty. In C#, you can use the below: public   static   byte [] CreateMD5Hash( List < string > dataList)         {              using  ( MemoryStream  fingerStream =  new   MemoryStream ())             {                  using  ( StreamWriter  writer =  new   StreamWriter (fingerStream))                 {                      foreach  ( string  data  in  dataLi...

Sending emails with attachments in .NET (Caveats)

This is quite easy to do, what is not so apparent though is that if you try to delete the attachment either in code or manually even after the mail is sent, it does not let you do it. This puzzled me for a while. Now, I realize - you need to dispose the MailMessage - if you don't then it hangs on to the file until your program restarts.

System.Threading.Timers.Timer (also) has issues

As many .NET developers know, we can't use System.Timers.Timer because it sometimes does not fire the events in time. Hence, we are recommended to use System.Threading.Timers.Timer. Yesterday I found one issue with this timer as well which I wanted to point out: http://msdn.microsoft.com/en-us/library/yz1c7148(v=vs.100).aspx If you pass infinite as the due time then on some servers, the timer just stops working and never fires again. I usually test with all windows updates (optional + important). On such a system this never happened. However, we did notice this happening on a test machine yesterday. It was a 2008 R2 VM. To get it working, I pass the same value as period to the due time. So far, this seems to be working. [Update] It does not work. I now stop the timer, reset the interval and start it again. Hopefully this works.

Visual Studio just killed my code changes (just after I tested it works perfectly) - how to get your code back.

Just decompile the .NET dll from where you were testing if you want to be 100% sure of the fact that you did not lose your changes.

How to reduce CPU usage when the code causes continous 80-100% CPU running normally.

In this example, I was trying to create hash codes for 14K files. The first thing to do is to try and use an algorithm which does not use that much CPU. In my case, that meant using a crc32 algorithm instead of the MS md5 class. But this just reduced the CPU for 100% to 80% - it still remained at 80% for half an hour or more. In such cases, we have to work around this. Because basically what is happening is that one by one if we generate the hash codes for so many files, it will consume that much CPU. The solution is to put Thread sleeps in between, and if you want like me, you can vary the sleep as per the size of the file, because the high CPU is caused not by the number of files alone, but by the size of the files. Put an adequate amount of sleep in between the hash code generation code so that, the CPU is able to do other work in between. You can experiment by changing the sleep and observing how the CPU goes up and down. I heard that using BeginInvoke instead of Task ba...

Calling a higher level method deep inside the lower levels of the code

I have encountered this problem many times in my career. Somewhere deep in the code where we have very little context, we need to use some parameters which are only available in the top layers of the code and then if you are really unlucky, you may even need to make a WCF call to another server. It is not very difficult to solve this problem. This is a scenario where you could "use a method like a variable". I.e., create a delegate which may take a parameter and may even return a value (if you need). Inside this method, you can write all the high level code you want. Func<string, string> MyAction = delegate (string var1) {       //Whatever you want to do, you can do here.       return "something"; }; - If you don't want to return anything, use Action<T> instead. - If you are within a for loop when you declare this delegate, do not use any list variables like list[i] inside this method. Set it to a real variable: var x = list[i] ...