How to: Execute action when data is received

Source: Internet
Author: User

?

This article is intended to demonstrate the use of actionblock.

Approximate process:

Input path--Read bytes--compute--Transfer to print

?

  1. Demonstrates how to provide delegates to exectution dataflow blocks.
  2. class Dataflowexecutionblocks
  3. {
  4. ???? //Calculation file contains a total of 0 bytes
  5. ???? Static int Countbytes (string path)
  6. ???? {
  7. ???????? byte [] buffer = newbyte[1024];
  8. ???????? int totalzerobytesread = 0;
  9. ???????? using (var fileStream = file.openread (path))
  10. ???????? {
  11. ???????????? int bytesread = 0;
  12. ???????????? Do
  13. ???????????? {
  14. ???????????????? Bytesread = filestream.read (buffer, 0, buffer. Length);
  15. ???????????????? Totalzerobytesread + = buffer. Count (b = = = 0);
  16. ????????????} while (Bytesread > 0);
  17. ????????}
  18. ?
  19. ???????? return Totalzerobytesread;
  20. ????}
  21. ?
  22. ???? Static void Run (string[] args)
  23. ???? {
  24. ???????? //Create a temp directory
  25. ???????? string tempfile = Path.gettempfilename ();
  26. ?
  27. ???????? //Random Write Data
  28. ???????? using (var fileStream = File.openwrite (tempfile))
  29. ???????? {
  30. ???????????? Random rand = new random ();
  31. ???????????? byte [] buffer = newbyte[1024];
  32. ???????????? for (int i = 0; i <; i++)
  33. ???????????? {
  34. ???????????????? Rand. Nextbytes (buffer);
  35. ???????????????? FileStream.Write (buffer, 0, buffer.) Length);
  36. ????????????}
  37. ????????}
  38. ?
  39. ???????? //Create a Actionblock<int> object to print the number of bytes read to
  40. ???????? var printresult = new actionblock<int> (zerobytesread =
  41. ???????? {
  42. ???????????? Console.WriteLine ("{0} contains {1} zero bytes.",
  43. ???????????????? Path.getfilename (tempfile), zerobytesread);
  44. ????????});
  45. ?
  46. ???????? //Genesis a transformblock<string, int> object to invoke the Countbytes function and return the result of the calculation
  47. ???????? var countbytes = New transformblock<string, int> (
  48. ???????????? New func<string, int> (countbytes));
  49. ?
  50. ???????? //Two blocks are linked together:tranformblock<string,int> objects and Actionblock objects.
  51. ???????? Countbytes.linkto (Printresult);
  52. ?
  53. ???????? //Create a continuous task: When transformblock<string, int> finishes, notifies the print result is complete
  54. ???????? CountBytes.Completion.ContinueWith (delegate {printresult.complete ();});
  55. ?
  56. ???????? //Enter temp directory
  57. ???????? Countbytes.post (Tempfile);
  58. ?
  59. ???????? //End of logo
  60. ???????? Countbytes.complete ();
  61. ?
  62. ???????? //wait for print to finish
  63. ???????? printResult.Completion.Wait ();
  64. ?
  65. ???????? File.delete (Tempfile);
  66. ????}
  67. }

?

Transformblock: Generally used for transmission and calculation. A map operation in a function-like programming.

Func<tinput,toutput> delegate, through post, enter a tinput parameter.

Complete (), indicating completed

Completion task, completed, the current task ends. Essentially, Tranformblock runs in a task.

?

Actionblock:

The Action<tinput> action delegate, a single input, performs operations such as printing.

There are also complete () and completion tasks.

?

At the same time, the above two support async methods:

Transform

Action

?

  1. var countbytesasync = New transformblock<string, int> (async Path = =
  2. {
  3. ???? byte [] buffer = newbyte[1024];
  4. ???? int totalzerobytesread = 0;
  5. ???? using (var fileStream = new fileStream (
  6. ???????? Path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, true))
  7. ???? {
  8. ???????? int bytesread = 0;
  9. ???????? Do
  10. ???????? {
  11. ???????????? //Asynchronously read from the file stream.
  12. ???????????? Bytesread = await fileStream. Readasync (buffer, 0, buffer.) Length);
  13. ???????????? Totalzerobytesread + = buffer. Count (b = = = 0);
  14. ????????} while (Bytesread > 0);
  15. ????}
  16. ?
  17. ???? return Totalzerobytesread;
  18. });

How to: Execute action when data is received

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.