?
This article is intended to demonstrate the use of actionblock.
Approximate process:
Input path--Read bytes--compute--Transfer to print
?
- Demonstrates how to provide delegates to exectution dataflow blocks.
- class Dataflowexecutionblocks
- {
- ???? //Calculation file contains a total of 0 bytes
- ???? Static int Countbytes (string path)
- ???? {
- ???????? byte [] buffer = newbyte[1024];
- ???????? int totalzerobytesread = 0;
- ???????? using (var fileStream = file.openread (path))
- ???????? {
- ???????????? int bytesread = 0;
- ???????????? Do
- ???????????? {
- ???????????????? Bytesread = filestream.read (buffer, 0, buffer. Length);
- ???????????????? Totalzerobytesread + = buffer. Count (b = = = 0);
- ????????????} while (Bytesread > 0);
- ????????}
- ?
- ???????? return Totalzerobytesread;
- ????}
- ?
- ???? Static void Run (string[] args)
- ???? {
- ???????? //Create a temp directory
- ???????? string tempfile = Path.gettempfilename ();
- ?
- ???????? //Random Write Data
- ???????? using (var fileStream = File.openwrite (tempfile))
- ???????? {
- ???????????? Random rand = new random ();
- ???????????? byte [] buffer = newbyte[1024];
- ???????????? for (int i = 0; i <; i++)
- ???????????? {
- ???????????????? Rand. Nextbytes (buffer);
- ???????????????? FileStream.Write (buffer, 0, buffer.) Length);
- ????????????}
- ????????}
- ?
- ???????? //Create a Actionblock<int> object to print the number of bytes read to
- ???????? var printresult = new actionblock<int> (zerobytesread =
- ???????? {
- ???????????? Console.WriteLine ("{0} contains {1} zero bytes.",
- ???????????????? Path.getfilename (tempfile), zerobytesread);
- ????????});
- ?
- ???????? //Genesis a transformblock<string, int> object to invoke the Countbytes function and return the result of the calculation
- ???????? var countbytes = New transformblock<string, int> (
- ???????????? New func<string, int> (countbytes));
- ?
- ???????? //Two blocks are linked together:tranformblock<string,int> objects and Actionblock objects.
- ???????? Countbytes.linkto (Printresult);
- ?
- ???????? //Create a continuous task: When transformblock<string, int> finishes, notifies the print result is complete
- ???????? CountBytes.Completion.ContinueWith (delegate {printresult.complete ();});
- ?
- ???????? //Enter temp directory
- ???????? Countbytes.post (Tempfile);
- ?
- ???????? //End of logo
- ???????? Countbytes.complete ();
- ?
- ???????? //wait for print to finish
- ???????? printResult.Completion.Wait ();
- ?
- ???????? File.delete (Tempfile);
- ????}
- }
?
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
?
- var countbytesasync = New transformblock<string, int> (async Path = =
- {
- ???? byte [] buffer = newbyte[1024];
- ???? int totalzerobytesread = 0;
- ???? using (var fileStream = new fileStream (
- ???????? Path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, true))
- ???? {
- ???????? int bytesread = 0;
- ???????? Do
- ???????? {
- ???????????? //Asynchronously read from the file stream.
- ???????????? Bytesread = await fileStream. Readasync (buffer, 0, buffer.) Length);
- ???????????? Totalzerobytesread + = buffer. Count (b = = = 0);
- ????????} while (Bytesread > 0);
- ????}
- ?
- ???? return Totalzerobytesread;
- });
How to: Execute action when data is received