Q: I've read some fantastic tips to easily develop asynchronous implementations using C # iterators. (Jeffrey Richter in the previous issue of MSDN® magazine) had highly praised such technology. However, I've tried to figure out how to do similar work without this kind of code conversion, which is really difficult. For example, let's say I want to copy from one stream to another only by using asynchronous methods on stream. I know how to make an asynchronous request (complete one read and one write), but how can I continue to loop through the request to handle the entire stream? Is that possible?
Q: I've read some fantastic tips to easily develop asynchronous implementations using C # iterators. (Jeffrey Richter in the previous issue of MSDN® magazine) had highly praised such technology. However, I've tried to figure out how to do similar work without this kind of code conversion, which is really difficult. For example, let's say I want to copy from one stream to another only by using asynchronous methods on stream. I know how to make an asynchronous request (complete one read and one write), but how can I continue to loop through the request to handle the entire stream? Is that possible?
A: Yes, maybe. I will use your example to explain how to do this through C # and visual basic® (note that Visual Basic does not provide compiler support for iterators). To set this stage, Figure 1 shows the synchronization implementation that we want to implement asynchronously, which can be invoked through the following code:
A: Yes, maybe. I will use your example to explain how to do this through C # and visual basic® (note that Visual Basic does not provide compiler support for iterators). To set this stage, Figure 1 shows the synchronization implementation that we want to implement asynchronously, which can be invoked through the following code:
Figure 1 Synchronous CopyStreamToStream
public static void CopyStreamToStream (
stream source, stream destination,
Action<stream, Stream, Except Ion> completed)
{
byte[] buffer = new byte[0x1000];
int read;
Try
{while
(read = source. Read (buffer, 0, buffer.) Length)) > 0)
{
destination. Write (buffer, 0, read);
}
if (completed!= null) completed (source, destination, null);
catch (Exception exc)
{
if (completed!= null) completed (source, destination, exc);
}
FileStream input = ...;
FileStream output = ...;
CopyStreamToStream (input, output, (Src,dst,exc) => {
src. Close ();
Dst. Close ();
});