Jump to content

Recommended Posts

Posted

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

******* }

 

 

More...

 

View All Our Microsoft Related Feeds

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...