Sunday 3 November 2013

Parallel.ForEach is good. List.ForEach is not.

I myself had used List.ForEach(Action action) many times believing that it was efficient and that it uses multiple threads. Apparently it does not :). Now that i know that it does not use multiple threads, i can think of one or two good reasons why it may not be doing so but none of those convincing enough.

On the other hand, Parallel.ForEach works as expected and uses machine's available power to the fullest. To test the difference between the two, i used following code:

public partial class TestClass
{
        internal void Method1()
        {

            System.Threading.Thread.Sleep(1);
        }
}

In the Main method, i wrote the following:

            TestClass[] testClasses = new TestClass[10000];
            for (int i = 0; i < testClasses.Length; i++)
            {
                testClasses[i] = new TestClass();
            }

            Stopwatch watch = new Stopwatch();
            watch.Start();
            Parallel.ForEach(testClasses, (t) => t.Method1());
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

            watch.Reset();
            watch.Start();
            List list = new List();
            list.AddRange(testClasses);
            list.ForEach(t => t.Method1());
            watch.Stop();

            Console.WriteLine(watch.ElapsedMilliseconds);

You can use process explorer to witness the spawning of multiple threads when Parallel extension is executed.

Mismatch in Message property between FileNotFoundException and ArgumentNullException

There is an interesting mismatch in content produced by Message property of FileNotFoundException and ArgumentNullException.

Say, your code does something like following:

throw new FileNotFoundException("Test error", "\\share\test.txt");

If you print the Message property of thrown exception, it prints "Test error" and does not provide any details about the value provided in FilePath parameter (i.e. \\share\test.txt).

On the other hand, if you code does something like following:

throw new ArgumentNullException("param", "message");

If you print the Message property of thrown exception, it prints details about both arguments - message and argument.

Somehow it does not seem consistent. We ran into a bit of problem because of this mismatch. For now, we resorted back to ex.ToString() as that prints the complete information.

TF80012 - Maybe there is nothing wrong

Last week i encountered a strange error. While trying to publish some items to TFS using Excel's TFS add-in, i had some network connectivity issues and post that error, whenever i tried to launch TFS query results to Excel using "Open in Microsoft Excel" option, i started to see following prompt.

TF80012: The document cannot be opened because there is a problem with the installation of the Microsoft Visual Studio Team Foundation Office integration components.  For more information, see the following page on the Microsoft website: http://go.microsoft.com/fwlink/?LinkId=220459.

When i visited the URL mentioned in the prompt, i did not find anything that made sense to me. It was strange because everything was working smoothly prior to the publishing failure caused by network connectivity issues. In the end, the solution to this problem was pretty simple.

Somehow, the COM add-in in Excel for TFS 2012 was disabled - maybe Excel found the last error caused by the add-in to be severe or something. When i opened a new Excel workbook, TEAM option was not showing up. All i needed to do was:

1. Open new Excel and select Options from File menu.
2. In the popup window, select Add-Ins, select "COM Add-In" in the "Manage" dropdown at the bottom of the popup window and press Go.
3. In the new popup window, select "Team Foundation Add-in" and press OK.

And everything is back to normal.