Synchronous Writing, Asynchronous Reading : Asynchronous Input output « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » Asynchronous Input outputScreenshots 
Synchronous Writing, Asynchronous Reading

//Code revised from Chapter 25 - Accessing Data Streams
//C# Bible
//by Jeff Ferguson et al. 

using System;
using System.IO;
using System.Threading;
   
class FileTestClass
{
    private static FileStream    BinaryFile = new FileStream("test.dat", FileMode.Create, FileAccess.ReadWrite);
    private static byte []       ByteArray = new byte [256];
    private static IAsyncResult  AsyncResultImplementation;
    private static AsyncCallback ReadBytesCompleteCallback= new AsyncCallback(OnReadBytesComplete);

    public static void OnReadBytesComplete(IAsyncResult AsyncResult)
    {
        int BytesRead;
        int Failures;
   
        BytesRead = BinaryFile.EndRead(AsyncResult);
        Failures = 0;
        for(int i = 0; i < 256; i++)
        {
            if(ByteArray[i!= (byte)i)
            {
                Console.WriteLine("Read test failed for byte at offset {0}.", i);
                Failures++;
            }
        }
    }
    static public void Main()
    {
        for(int i = 0; i < 256; i++)
            ByteArray[i(byte)i;
        BinaryFile.Write(ByteArray, 0256);

        BinaryFile.Seek(0, SeekOrigin.Begin);
        AsyncResultImplementation = BinaryFile.BeginRead(ByteArray, 0256, ReadBytesCompleteCallback, null);

        WaitHandle WaitOnReadIO = AsyncResultImplementation.AsyncWaitHandle;
        WaitOnReadIO.WaitOne();
    }    
}

           
       
Related examples in the same category
1.Asynchronous Writing, Asynchronous Reading
2.illustrates asynchronous I/Oillustrates asynchronous I/O
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.