Click here to Skip to main content
15,897,704 members
Articles / High Performance Computing / Parallel Processing

Parallel Extensions for the .NET Framework - Part I (Introduction & PLINQ)

Rate me:
Please Sign up or sign in to vote.
4.96/5 (17 votes)
14 Nov 2008CPOL8 min read 70.9K   953   47  
This article introduces PFX for the .NET Framework and discusses PLINQ.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Threading;

namespace Plinq
{
    class Program
    {
       
        static void Main(string[] args)
        {
            try
            {
                SampleForm frm = new SampleForm();
                frm.ShowDialog();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                Console.ReadLine();
            }
        }

    }

    public class SampleQuery
    {
        public List<FileInformation> fileList = new List<FileInformation>();
        enum OutputProcessor { PipeLine, StopnGet, Inverted };

        #region Collection and XML File generation
        public  void InitializeList()
        {
            if (fileList == null || fileList.Count == 0)
            {
                string dataPath = Path.GetFullPath(@"..\..\Data\");
                string fileListPath = Path.GetFullPath(Path.Combine(dataPath, "FilesList.xml"));
                if (!File.Exists(fileListPath))
                {
                    Console.WriteLine("Data file doesnot exist. Press Y if you want to create. N will exit.");
                    string strYN = Console.ReadLine();
                    if (strYN.ToUpper() == "Y")
                    {
                        CreateXMLFile();
                    }
                    else
                    {
                        return;
                    }
                }
                fileList = (
                    from e in XDocument.Load(fileListPath).
                              Root.Elements("File").AsParallel()
                    select new FileInformation
                    {
                        FileName = e.Attribute("Name").Value as string,
                        FileLocation = e.Attribute("Location").Value as string,
                        CreationTime = DateTime.Parse(e.Attribute("CreationTime").Value),
                        LastWriteTime = DateTime.Parse(e.Attribute("LastWriteTime").Value),
                        Length = long.Parse(e.Attribute("Length").Value)
                    })
                    .ToList();
            }
        }
        private  void CreateXMLFile()
        {
            string[] arrDrives = new string[] { "C:\\" };
            XDocument xDoc = new XDocument();
            XElement xElement = new XElement("Files");
            SearchDirectory(arrDrives, xElement);
            xDoc.Add(xElement);
            xDoc.Save(Path.GetFullPath(@"..\..\Data\"));
        }
        private  void SearchDirectory(string[] arrDrives, XElement xElement)
        {
            Parallel.ForEach(arrDrives, (string strDrive) =>
            {
                try
                {
                    if (strDrive == "C:\\Windows" || strDrive == "C:\\Program Files")
                        return;
                    System.Diagnostics.Debug.WriteLine(strDrive);
                    string[] files = Directory.GetFiles(strDrive);
                    foreach (string file in files)
                    {
                        FileInfo fInfo = new FileInfo(file);
                        XElement xelem = new XElement("File",
                                                        new XAttribute("Location", strDrive),
                                                        new XAttribute("Name", file),
                                                        new XAttribute("LastWriteTime", fInfo.LastWriteTime),
                                                        new XAttribute("CreationTime", fInfo.CreationTime),
                                                        new XAttribute("Length", fInfo.Length));
                        xElement.Add(xelem);
                    }

                    string[] folders = Directory.GetDirectories(strDrive);
                    SearchDirectory(folders, xElement);
                }
                catch
                {
                }

            });
        }
        #endregion

        #region Standard Query Operator
        public void ExecuteSimpleWhere()
        {
        }

        public void ExecuteComplexWhere()
        {
        }

        public void ExecuteAllGroup()
        {
        }

   

        #endregion

        #region Parallel Query Operator
        //public void ExecuteSimpleWhereA()
        //{
        //}

        //public void ExecuteComplexWhere()
        //{
        //}

        //public void ExecuteAllGroup()
        //{
        //}

        //public void ExecuteComplexWhere()
        //{
        //}

        #endregion
    }


    public class FileInformation
    {
        public string FileName { get; set; }
        public string FileLocation { get; set; }
        public DateTime LastWriteTime { get; set; }
        public DateTime CreationTime { get; set; }
        public long Length { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer SAP Labs
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions