3 using System
.Threading
;
10 static void async_callback (IAsyncResult ar
)
12 byte [] buf
= (byte [])ar
.AsyncState
;
13 Interlocked
.Add (ref sum
, buf
[0]);
14 Interlocked
.Increment (ref count
);
18 byte [] buf
= new byte [1];
19 AsyncCallback ac
= new AsyncCallback (async_callback
);
24 FileStream s
= new FileStream ("async_read.exe", FileMode
.Open
, FileAccess
.Read
);
27 while (s
.Read (buf
, 0, 1) == 1) {
36 ar
= s
.BeginRead (buf
, 0, 1, ac
, buf
);
37 } while (s
.EndRead (ar
) == 1);
43 count0
++; // async_callback is invoked for the "finished reading" case too
44 Console
.WriteLine ("CSUM: " + sum
+ " " + sum0
);
45 Console
.WriteLine ("Count: " + count
+ " " + count0
);
46 if (sum
!= sum0
|| count
!= count0
)