. NET related issues: asynchronous stream processing

Source: Internet
Author: User

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 ();
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.