Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 3.41 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace FileCombiner
  9. {
  10.     class Program
  11.     {
  12.         private static string _dirPath = @"C:\GIT\FileCombiner\FileCombiner\Files";
  13.         private static string _outFile = Path.Combine(_dirPath, @"Output\output.txt");
  14.         static void Main(string[] args)
  15.         {
  16.             File.Delete(_outFile);
  17.             var output = new OutputWriter(_outFile);
  18.             var eventLoop = new EventLoop();
  19.             eventLoop.Start();
  20.             eventLoop.Enqueue( () => Directory
  21.                                          .GetFiles( _dirPath )
  22.                                          .AsParallel()
  23.                                          .ForAll( file =>
  24.                                                       {
  25.                                                           File.ReadAllLines( file )
  26.                                                               .AsParallel()
  27.                                                               .ForAll(
  28.                                                                   line => eventLoop.Enqueue(() => output.WriteOutput( String.Format("{0}\t{1}",file,line) ) ) );
  29.                                                           ;
  30.                                                       } ));
  31.             // not worried about completion notice at the moment - just waiting for sample file to hit 4290k.
  32.             Console.WriteLine("Processing....press Enter to exit when complete");
  33.             Console.ReadLine();
  34.         }
  35.     }
  36.  
  37.     public class OutputWriter
  38.     {
  39.         private readonly Object _lockSync = new Object();
  40.         private readonly string _path;
  41.  
  42.         public OutputWriter(string path)
  43.         {
  44.             _path = path;
  45.         }
  46.  
  47.         public void WriteOutput( string line )
  48.         {
  49.             lock(_lockSync) // Yep, locks every time - enough for example
  50.             {
  51.                 using(var fs = File.AppendText(_path))
  52.                 {
  53.                     fs.WriteLine(line);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     public class EventLoop
  60.     {
  61.         public bool Running { get; set; }
  62.         public ConcurrentQueue<Action> ActionQueue { get; set; }
  63.         public ManualResetEventSlim Wait { get; set; }
  64.        
  65.         public void Loop()
  66.         {
  67.             while( Running )
  68.             {
  69.                 Action action = null;
  70.                 if( ActionQueue.TryDequeue( out action ) )
  71.                 {
  72.                     try
  73.                     {
  74.                         Task.Factory.StartNew( action );
  75.                     }
  76.                     catch( Exception ex )
  77.                     {
  78.                         Console.WriteLine( ex );
  79.                     }
  80.                 }
  81.                 else
  82.                 {
  83.                     Wait.Reset();
  84.                     Wait.Wait();
  85.                 }
  86.             }
  87.         }
  88.  
  89.         public void Enqueue( Action action )
  90.         {
  91.             ActionQueue.Enqueue( action );
  92.             Wait.Set();
  93.         }
  94.  
  95.         public void Start()
  96.         {
  97.             Running = true;
  98.             Task.Factory.StartNew(Loop);
  99.         }
  100.  
  101.         public void Stop()
  102.         {
  103.             Running = false;
  104.             Wait.Set();
  105.         }
  106.  
  107.         public EventLoop( )
  108.         {
  109.             ActionQueue = new ConcurrentQueue<Action>();
  110.             Wait = new ManualResetEventSlim( false );
  111.         }
  112.     }
  113. }