Hey guys, I am playing with the .NET 4.0's new class Parallel.

I tried to open files in a directory and calculate the total bytes of them. However when I run the code, I get a different result every time.

Can you explain me the problem I have?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Drawing;

namespace LearnCSharp
{

    class Program
    {
        static long totalBytes = 0;

        private static object token = new object();

        static void ProcessImages()
        {
            string directoryPath = @"C:\files";

            string[] fileNames = Directory.GetFiles(directoryPath);

            if (fileNames == null)
                throw new Exception("Path is wrong");

            ParallelLoopResult result = Parallel.ForEach<string>(fileNames, new Action<string>(
                delegate(string fileName)
                {   
                    FileStream stream = File.OpenRead(fileName);
                    if (stream == null)
                        throw new Exception("File not found");

                    lock (token)
                    {
                        totalBytes =+ stream.Length;  
                    }
                    
                }
            ));

        }

        static void Main(string[] args)
        {
            TaskFactory taskFactory = new TaskFactory();
            Task task =  taskFactory.StartNew(new Action(ProcessImages));

            Task.WaitAll(task);

            Console.WriteLine(totalBytes);

            Console.Read();
        }
    }
}

Is this code copy + pasted? I have never seen a =+ operator before (as on line 39). How are the results different every time?

Okay, you can assume it as +=.

The results I get are like 150907, 220769, 78905 and so on. They are different each time I run the application. I do not add or remove any files from the directory.

What can be the problem other than =+ :D

@skatamatic, Wow the stupid mistake you found in the code was the actual problem.

Thank you :)

Lol glad I could help. I am curious why this even compiled though....

Edit: I typed that into visual studio and it kind of makes sense
Basically the + is being thought of as a positive integer (rather than - indicating a negative).
So totalBytes =+ stream.Length means totalBytes = 0 + stream.Length. Therefore TotalBytes equals whichever parallel task's stream size that finishes last.

Just to clarify that, the + is the unary +, just like there is the unary - (for example, x = x + -1, x = x + +1). It's rarely used, as in I've never seen anyone use it :)

commented: Beats my explanation lol. +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.