Click here to Skip to main content
13,349,033 members (70,967 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 11 Aug 2003

Event notification on streams during long reads or writes

, 11 Aug 2003
Rate this:
Please Sign up or sign in to vote.
An article on event notification for operations on large streams...

Sample screenshot


Sometimes during programming, there are some methods that take a stream and then perform some function on it. Usually, if the stream is fairly large, then the method may not return for an extreme amount of time (e.g. ~20 minutes for SHA1 calculation on 2 GB file across a network). Some applications would simply like to get notified that there was some activity and hopefully the amount of activity. Being notified that a block of bytes is about to, or has been read would solve this problem. Thus, the EventStream reader attempts to solve this problem. See the example for more information.


Using the code

Below is an example usage of this class, to be notified during a potentially long SHA1 calculation.

class ExampleNotifyClass
    public int timesNotified = 0 ;

    public void SectionWasRead( byte[] buff, int index, int count ) 
        Console.WriteLine("Section read: {0}", count ) ;
        timesNotified++ ;

    /// Main
    public static void Main() {

      // Create the sample data to read
      MemoryStream mem  = new MemoryStream() ;
      StreamWriter writer = new StreamWriter(mem, Encoding.UTF8) ;

      // Populate the stream
      for( int i = 0 ; i < 16 * 777 ; i++ ) 
        writer.WriteLine( "{0}", i % 10 ) ;

      // Reset the pointer in the stream
      mem.Seek( 0, SeekOrigin.Begin ) ;

      // Create a sample class 
      ExampleNotifyClass toNotify = new ExampleNotifyClass(); 
      EventStream toRead = new EventStream( mem ) ;
      toRead.AfterBlockRead += new 
        EventStream.AfterBlockHandler(toNotify.SectionWasRead ) ;
      toRead.BlockEventSize = 8192 ; 
        // Get notified for every 8192 bytes read

      SHA1 sha = new SHA1Managed() ;

      // Actual work is done here
      byte[] hash = sha.ComputeHash( toRead ) ;

      Console.WriteLine( "Our class was notified {0} times.", 
                             toNotify.timesNotified ) ;



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jim Wiese (aka Spunk)
Web Developer
United States United States
I generally attend most of the Microsoft DevDays in the south bay area (CA) and BayArea.NET functions in case any of you attend those as well. I'm always up for a lively disucussion about new technologies in the industry, Microsoft or not. Send me a note if you attend!

You may also be interested in...


Comments and Discussions

QuestionCool idea what about the asynchronous case? Pin
mkonrad19-Aug-03 21:17
membermkonrad19-Aug-03 21:17 
I do like your solution, although it would be nice if you would also implement the asynchronous part of the stream. So that one could be notificated in the asynchronous case.
By the way. I think you would have got a far better rating if you had included the eventstream class into the article (with explanation)
GeneralThanks! Pin
sichbo218-Aug-03 16:16
membersichbo218-Aug-03 16:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 12 Aug 2003
Article Copyright 2003 by Jim Wiese (aka Spunk)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid