public void StartTreeBuilding(string targetFolderPath, string outputFilePath)
        {
            try
            {
                _tasksFinished = 0;

                _infoCollector = new FilesInfoCollector(targetFolderPath);
                _infoNotifier = new FilesInfoNotifier();
                _xmlWriter = new XmlInfoWriter(outputFilePath);

                _infoCollector.GotNewFileSystemData += GotNewFileSystemDataHandler;
                _infoCollector.TaskFinished += TaskFinishedHandler;
                _infoCollector.ErrorOccurred += ErrorOccurredHandler;
                _infoNotifier.NewFileInfoAdded += NewFileInfoAddedHandler;
                _infoNotifier.TaskFinished += TaskFinishedHandler;
                _infoNotifier.ErrorOccurred += ErrorOccurredHandler;
                _xmlWriter.TaskFinished += TaskFinishedHandler;
                _xmlWriter.ErrorOccurred += ErrorOccurredHandler;

                var infoCollectorThread = new Thread(_infoCollector.Process) {IsBackground = true, Name = "InfoCollectorThread"};
                var infoNotifierThread = new Thread(_infoNotifier.ProcessTask) {IsBackground = true, Name = "InfoNotifierThread"};
                var xmlWriterThread = new Thread(_xmlWriter.ProcessTask) {IsBackground = true, Name = "XmlWriterThread"};

                infoCollectorThread.Start();
                infoNotifierThread.Start();
                xmlWriterThread.Start();
            }
            catch (Exception ex)
            {
                OnErrorOccurred(new ErrorEventArgs("Error occured while starting tree building.", ex));
            }
        }
 /// <summary>
 /// Handle ending of each task.
 /// </summary>
 private void TaskFinishedHandler(object sender, EventArgs e)
 {
     if (sender == _infoCollector)
     {
         _infoNotifier.SetQueueFillingFinished();
         _xmlWriter.SetQueueFillingFinished();
     }
     Interlocked.Increment(ref _tasksFinished);
     // Check if all tasks finished
     if (_tasksFinished == 3)
     {
         OnTreeBuildingFinished();
         _infoCollector = null;
         _infoNotifier  = null;
         _xmlWriter     = null;
     }
 }
        public void StartTreeBuilding(string targetFolderPath, string outputFilePath)
        {
            try
            {
                _tasksFinished = 0;

                _infoCollector = new FilesInfoCollector(targetFolderPath);
                _infoNotifier  = new FilesInfoNotifier();
                _xmlWriter     = new XmlInfoWriter(outputFilePath);

                _infoCollector.GotNewFileSystemData += GotNewFileSystemDataHandler;
                _infoCollector.TaskFinished         += TaskFinishedHandler;
                _infoCollector.ErrorOccurred        += ErrorOccurredHandler;
                _infoNotifier.NewFileInfoAdded      += NewFileInfoAddedHandler;
                _infoNotifier.TaskFinished          += TaskFinishedHandler;
                _infoNotifier.ErrorOccurred         += ErrorOccurredHandler;
                _xmlWriter.TaskFinished             += TaskFinishedHandler;
                _xmlWriter.ErrorOccurred            += ErrorOccurredHandler;

                var infoCollectorThread = new Thread(_infoCollector.Process)
                {
                    IsBackground = true, Name = "InfoCollectorThread"
                };
                var infoNotifierThread = new Thread(_infoNotifier.ProcessTask)
                {
                    IsBackground = true, Name = "InfoNotifierThread"
                };
                var xmlWriterThread = new Thread(_xmlWriter.ProcessTask)
                {
                    IsBackground = true, Name = "XmlWriterThread"
                };

                infoCollectorThread.Start();
                infoNotifierThread.Start();
                xmlWriterThread.Start();
            }
            catch (Exception ex)
            {
                OnErrorOccurred(new ErrorEventArgs("Error occured while starting tree building.", ex));
            }
        }
 /// <summary>
 /// Handle ending of each task.
 /// </summary>
 private void TaskFinishedHandler(object sender, EventArgs e)
 {
     if (sender == _infoCollector)
     {
         _infoNotifier.SetQueueFillingFinished();
         _xmlWriter.SetQueueFillingFinished();
     }
     Interlocked.Increment(ref _tasksFinished);
     // Check if all tasks finished
     if (_tasksFinished == 3)
     {
         OnTreeBuildingFinished();
         _infoCollector = null;
         _infoNotifier  = null;
         _xmlWriter = null;
     }
 }