NewsBot
1
Here is a chunky version of a large file copy using all async calls and CCr.* Can be modifed to any in or out stream type. Note we don't ever expect to see same number of bytes each read - so this pattern works with network streams too that often return less then number of bytes we request per read.*
******* private void button11_Click(object sender, EventArgs e)
******* {
*********** DispatcherQueue dq = new DispatcherQueue();
*********** Arbiter.Activate(dq,
*************** Arbiter.FromIteratorHandler(()=> CcrCopyFile(@"c:\temp\file1.exe", @"c:\temp\file2.exe")));
******* }
******* ///
******* /// Use CCR to copy file async in 8K chunks using iterator method.
******* ///
******* IEnumerator CcrCopyFile(string inPath, string outPath)
******* {
*********** var resultPort = new Port();
*********** long totCopied = 0;
*********** using (FileStream fsIn = new FileStream(inPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192, true))
*********** using (FileStream fsOut = new FileStream(outPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
*********** {
*************** byte[] buf = new byte[8192];
*************** int bytesRead = 0;
*************** do
*************** {
******************* // Read.
******************* IAsyncResult ar = null;
******************* fsIn.BeginRead(buf, 0, buf.Length - 1, resultPort.Post, null);
******************* yield return Arbiter.Receive(false, resultPort, (IAsyncResult ar2) => ar = ar2);
******************* bytesRead = fsIn.EndRead(ar);
******************* // Write.
******************* fsOut.BeginWrite(buf, 0, bytesRead, resultPort.Post, null);
******************* yield return Arbiter.Receive(false, resultPort, (IAsyncResult ar2) => ar = ar2);
******************* fsOut.EndWrite(ar);
******************* totCopied += bytesRead;
*************** } while (bytesRead > 0);
*********** }
*********** Console.WriteLine("Total bytes copied:{0}\nIn:{1}\nOut:{2}", totCopied, inPath, outPath);
******* }
******* private void button11_Click(object sender, EventArgs e)
******* {
*********** DispatcherQueue dq = new DispatcherQueue();
*********** Arbiter.Activate(dq,
*************** Arbiter.FromIteratorHandler(()=> CcrCopyFile(@"c:\temp\file1.exe", @"c:\temp\file2.exe")));
******* }
******* ///
******* /// Use CCR to copy file async in 8K chunks using iterator method.
******* ///
******* IEnumerator CcrCopyFile(string inPath, string outPath)
******* {
*********** var resultPort = new Port();
*********** long totCopied = 0;
*********** using (FileStream fsIn = new FileStream(inPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192, true))
*********** using (FileStream fsOut = new FileStream(outPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
*********** {
*************** byte[] buf = new byte[8192];
*************** int bytesRead = 0;
*************** do
*************** {
******************* // Read.
******************* IAsyncResult ar = null;
******************* fsIn.BeginRead(buf, 0, buf.Length - 1, resultPort.Post, null);
******************* yield return Arbiter.Receive(false, resultPort, (IAsyncResult ar2) => ar = ar2);
******************* bytesRead = fsIn.EndRead(ar);
******************* // Write.
******************* fsOut.BeginWrite(buf, 0, bytesRead, resultPort.Post, null);
******************* yield return Arbiter.Receive(false, resultPort, (IAsyncResult ar2) => ar = ar2);
******************* fsOut.EndWrite(ar);
******************* totCopied += bytesRead;
*************** } while (bytesRead > 0);
*********** }
*********** Console.WriteLine("Total bytes copied:{0}\nIn:{1}\nOut:{2}", totCopied, inPath, outPath);
******* }
More...
View All Our Microsoft Related Feeds