Tuesday, January 26, 2016

Anonymous method with Parameters 'i' -> Thread -> Iteration with 'i' -> Mess!

You see IndexOutOfRangeException with message  "Index was outside the bounds of the array." when you thread out an anonymous method with iterator value as parameter.

Surprised? How is it possible to see value i >= 4 (it was 4) in the 'for' loop there?
Well, it's 'possible' if your have written code as above.

Secret is that the anonymous method doesn't get called unless the Thread created doesn't becomes live. Once thread becomes live, it searches for it's anonymous method which is: MethodToCompute(NumberOfIterationPerThread[i])
Here, value of 'i' might have changed as 'for' loop to create thread runs in a different thread!

solution is NOT to pass any value to threaded anonymous method which the parent thread may modify. In above case, I would create an extra variable to store value to pass to anonymous method which will be threaded out.

    for (int i = 0; i < NumberOfThreads; i++)
        int iterationToRun = NumberOfIterationPerThread[i]; //magic line!
        threads[i] = new Thread(new ThreadStart(() => MethodToCompute(iterationToRun)));
        threads[i].IsBackground = true;

Wednesday, June 10, 2015

NDK Directory Location on Windows 7

I am not big fan of Android on Windows 7 but that's what I've at present and now I've to install NDK for some reason. So, I downloaded Android NDK from https://developer.android.com/ndk/downloads/index.html According to the documentation, I've to double click the NDK download and it will automatically extract file. I ran and now I don't know where it has extracted the files are. I have to mention installed NDK path (ndk.dir) in "local.properties" (btw, I am using Android Studio). After searching whole C: suing below DOS command (I saw my nkd folder was "android-ndk-r10e"),

    dir /b /s /ad | findstr "android-ndk-r10e"

Found the directory. As I've downloaded 64 bit NDK, it's


on my 64bit Windows 7 machine.

Hope this helps and you don't have to search C: like me.

Friday, September 19, 2014

The Ultimate Rounding Function

Your search for any requirement for rounding function is over. You need to round amount to quarter? dime? nickel? penny? a dollar? 10 dollar? Below function of single linen can provide you more than what you are looking for. for sure!
 decimal NearestOfPenny(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 0.01m, 0.5m);

 decimal NearestOfNickel(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 0.05m, 0.5m);

 decimal NearestOfDime(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 0.10m, 0.5m);

 decimal NearestOfQuarter(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);

 decimal NearestOfDollar(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 1m, 0.5m);

 decimal UpwardDollarOnlyIfReminderIsMoreThan70Cents(decimal amountToRound)
     return UltimateRoundingFunction(amountToRound, 1m, 0.3m); 
     //i.e. 0.70 will round up. but 0.69 will be rounded to 0.. this magic is from value of 'fairness'.
 //amountToRound => input amount
 //nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
 //fairness => btween 0 to 0.9999999___. 
 //            0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
 //            0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
 //            0.4999999... Non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
 //            0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
 decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
     return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;

Saturday, August 30, 2014

Completely Read-Only Object as Return Value

NOTE: I am not proposing a 'secure' code. It's just to make your code dumb proof, not hack proof.
At the pinnacle of the insanity, when you have that bong affecting your normal senses, you may want to return that object from your beloved method which cannot be modified in anyway. You cannot even create a new object of the class that you are returning to have exclusive access to your return type. Anyways, whatever it can be used (?) for, it's here:
using System;
using System.Collections.ObjectModel;
using System.Security.Permissions;

public class ReadOnlyClassAsReturnValue
    private int sampleInt;
    // make regular property read-only by creating only 'get' call 
    public int SampleInt
        get { return sampleInt; }

    private bool[] sampleCollection;
    // Collections need to be made ReadOnlyCollection<T> . 
    //      NOTE: Though ReadOnlyCollection<T> can be casted as IList<T>, never 
    //          do that. It allows someone to to change collection in compilte
    //          time only to realized during runtime that it cannot be changed
    public ReadOnlyCollection<bool> SampleCollection
        get { return Array.AsReadOnly(sampleCollection); } //gives ReadOnlyCollection<T>

    // 'private' varialbes can only be assigned in contructor. If any variable need to be assigned after
    // object is created, it need to be made 'internal'.
    protected string sampleString = "x";
    public string SampleString
        get { return sampleString; }

    // Contructor is made 'protected' as this class is to be used by serious consumers only.
    // If ultimate (dumb) user is going to be outside your assembly, you can make it 
    // 'internal' instead of 'prorected' eliminating need to override this class and 
    // providing more protection than 'protected' (and 'proected internal').
    protected ReadOnlyClassAsReturnValue(int sampleInt, bool[] sampleCollection)
        this.sampleInt = sampleInt;
        this.sampleCollection = sampleCollection;

public class PrimaryUserClass
    // Inherit ReadOnlyClassAsReturnValue so internal methods/properties/variables can be assigned
    private class ReadOnlyObjectMaker : ReadOnlyClassAsReturnValue
        public ReadOnlyObjectMaker(int sampleInt, bool[] sampleCollection)
            : base(sampleInt, sampleCollection)
        { }

        public string SampleString
                this.sampleString = value;

    public ReadOnlyClassAsReturnValue ReadOnlyClassReturner()
        ReadOnlyObjectMaker returnValue = new ReadOnlyObjectMaker(10, new bool[] { true, false });
        returnValue.SampleString = "I can assign this";
        return returnValue;

class Program
    static void Main(string[] args)
        var returnValue = new PrimaryUserClass().ReadOnlyClassReturner();
        //returnValue cannot be modified. Why? Just for fun. Someone has to hack it to change anythingin returnValue.

Tuesday, February 18, 2014

Counting Number of REAL lines in Your C# Code

If you are here, it means one (or more) of the three things: 1. You are the boss, 2. Your boss told you to count number of lines 3. You are a serious programmer.
Anyways, let's come to the point. Each programer is different.  Problem with just literally counting the line has many issues which this piece of code attempts to resolve as mentioned below:
0. Comments of // and /* kind are ignored.
1. A statement written in multiple line is considered single line.
2. brackets are (i.e. '{') not considered lines.
3. 'using namespace' line are ignored.

        private int CountNumberOfLinesInCSFilesOfDirectory(string dirPath)
            FileInfo[] csFiles = new DirectoryInfo(txtPath.Text.Trim())
                                        .GetFiles("*.cs", SearchOption.AllDirectories);

            int totalNumberOfLines = 0;
            Parallel.ForEach(csFiles, fo =>
                Interlocked.Add(ref totalNumberOfLines, CountNumberOfLine(fo));
            return totalNumberOfLines;

        private int CountNumberOfLine(Object tc)
            FileInfo fo = (FileInfo)tc;
            int count = 0;
            int inComment = 0;
            using (StreamReader sr = fo.OpenText())
                string line;
                while ((line = sr.ReadLine()) != null)
                    if (IsRealCode(line.Trim(), ref inComment))
            return count;

        private bool IsRealCode(string trimmed, ref int inComment)
            if (trimmed.StartsWith("/*") && trimmed.EndsWith("*/"))
                return false;
            else if (trimmed.StartsWith("/*"))
                return false;
            else if (trimmed.EndsWith("*/"))
                return false;

                   inComment == 0
                && !trimmed.StartsWith("//")
                && (trimmed.StartsWith("if")
                    || trimmed.StartsWith("else if")
                    || trimmed.StartsWith("using (")
                    || trimmed.StartsWith("else  if")
                    || trimmed.Contains(";")
                    || trimmed.StartsWith("public") //method signature
                    || trimmed.StartsWith("private") //method signature
                    || trimmed.StartsWith("protected") //method signature

There is (literally) no solution for below (un)kind of programers:
1. Write multiple statements in single line.
2. Who declare useless variables.
3.Whose every other line is debug.write
4. Inappropriate use of compiler directive (directive itself is not considered as line btw)
5. Bulk copy programmers who suffer from schizophrenia and lives only in their set of classes. Don't trust or reuse anything else.
6. Who still likes to reinvent the wheel.

Apart from above six non-solvable issues, if you can improve my code, it will be helpful to everyone.

Thursday, April 04, 2013

Firefox 20 Feature - Click To Play

If you, my friend, are annoyed by zillions of flash advertisements running on your browser for which you haven't subscribed, have a look at the latest version of Firefox 20. It has a hidden lovable feature called "Click To Play". If enabled, it will deactivate all Flash (and Java) plugins except the one which you select to activate in a page. I've a netbook ('cheap laptop') which cannot handle the army of flash advertisements so I chose to activate this hidden Click_To_Play feature on my recently updated Firefox.

How to Enable Click_To_Play:

  1. Open a new firefox tab and type "about:config" in address bar.
  2. Click on button which says "I will be careful" etc..
  3. Now search for Click_To_Play in "Search" box.
  4. Double click preference named "plugins.click_to_play". It will change value of the preference to 'true'.
  5. You are done. Go to any web site and click only the plugin which you know you want to activate.  
Happy Browsing and don't let the Flash ads bite!

Friday, February 15, 2013

Auto Mouse Click and Drag Generator

If you are looking for a simple but powerful fast mouse clicker application, you are not alone. I am growing old and the software written by kids are too difficult for me to use, so I wrote a simple one called Auto Mouse Click and Drag Generator by myself which suite my work style - use keyboard when you automate mouse.

Here is from where you can try yourself: https://sourceforge.net/projects/superclick/files/latest/download?source=directory

Source Code can be found at project home: https://sourceforge.net/projects/superclick/?source=directory

Dedicated blog location is: http://automouseclicker.blogspot.in/
Below are the screens of the software as of today. Latest version may have more features and better UI. I don't intend to post update on this blog as the software has it's own dedicated blog.

Happy clicking and crashing others' software!

Unity3D and Vector Graphics

SVG is a popular vector graphics format. It has same limitation and advantages like any other vector graphics formats but SVG is very popular (and it's open format). Actually most modern browsers (and mobile devices) support it. Also, a graphics developer can create complex vector graphics using various tools available and need not to be programmed. They are not there to replace all raster format but in some specific cases, it may be more advantageous to use vector graphics. One of the major advantage of the vector graphics in general is that it looks crisp regardless of resolution, and they are less in size (well, not all the times). They may take same amount of memory and little more processing power but sometime they are worth it.

After thinking about all these advantages, I went look for unity plugin that can support direct rendering of SVG file or converts it to texture in runtime and show it and I found this:


This one is still in its basic form and not yet practical to use. I hope something along the way come to make Unity use 2D vector rendering easier.

The other one that I stuck during my search was:
This one works with basic shapes and creates triangles out of vector forms which is more Unity friendly. It's nice thing but the implementation as of now has very limited features and most probably will not satisfy your needs.

Btw, as you may know, Flash already supports vector rendering (proprietary format) and this is the main reason why Flash games are very light in size and still looks crisp.