Home > Development > APM Techniques with FileStream

APM Techniques with FileStream


using System;
using System.IO;
using System.Linq;
using System.Text;

namespace AsynchronousProgrammingModelSamples
{
    public class Program
    {
        private string path = @"C:\test.txt";

        static void Main()
        {
            Program p = new Program();
            p.WriteContentsToFile();
           // p.ProcessLinesWithAPMUsingBlockWait();
           // p.ProcessLinesWithAPMUsingPolling();
            p.ProcessLinesWithAPMUsingCallbacks();
            Console.WriteLine("Press any key to quit..");
            Console.ReadKey();
        }

        public void ProcessLinesWithAPMUsingCallbacks()
        {
            FileStream stream = null;
            byte[] data = null;
            try
            {
                stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous);
                data = new byte[stream.Length];
                stream.BeginRead(data, 0, data.Length, x =>
                                                           {
                                                               Console.WriteLine(Encoding.ASCII.GetString(data));
                                                           }, null);
            }
            catch (Exception e)
            {

            }
            finally
            {
                if (stream != null) stream.Close();
            }
        }

        public void ProcessLinesWithAPMUsingPolling()
        {
            FileStream stream = null;
            byte[] data = null;
            try
            {
                stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous);
                data = new byte[stream.Length];
                IAsyncResult result = stream.BeginRead(data, 0, data.Length, null, null);

                while(!result.IsCompleted) // Pool and do something - Not a great way to do things 🙂
                {
                    Console.WriteLine("Waiting ....");
                }


                stream.EndRead(result);

                Console.WriteLine(Encoding.ASCII.GetString(data));

            }
            catch (Exception e)
            {

            }
            finally
            {
                if (stream != null) stream.Close();
            }
        }

        public void ProcessLinesWithAPMUsingBlockWait()
        {
            FileStream stream = null;
            byte[] data = null;
            try
            {
                stream = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read,1024,FileOptions.Asynchronous);
                data = new byte[stream.Length];
                IAsyncResult result = stream.BeginRead(data, 0, data.Length, null, null);

                stream.EndRead(result); // Basically we are blocking here util finished so no difference to the synch
                                        // version

                Console.WriteLine(Encoding.ASCII.GetString(data));

            }
            catch (Exception e)
            {

            }
            finally
            {
                if(stream != null) stream.Close();
            }
        }

        public void WriteContentsToFile()
        {
            var items = Enumerable.Range(1, 1000)
                                  .Select(x => x.ToString())
                                  .ToArray();

            var text = String.Join(Environment.NewLine, items);
            File.WriteAllText(path, text);

        }
    }
}

Blair…

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: