Just realized that code could benefit from some sample usage...
-S
static void CopyLargeFile(string source, string destination, bool wait)
{
int buffersize = 65536; //ymmv
bool useasync=true; //required!
using (Stream src = new FileStream(
source,FileMode.Open,FileAccess.Read,FileShare.Read,
buffersize,useasync))
using (Stream dst = File.Create(destination,buffersize))
{
Jitsu.IO.AsyncStreamPump pump =
new Jitsu.IO.AsyncStreamPump(buffersize);
pump.InboundStream = src;
pump.OutboundStream = dst;
pump.Start();
if (wait)
pump.DoneSignal.WaitOne();
return;
}
}
-----Original Message-----
From: Shawn A. Van Ness [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 19, 2004 22:53
To: [EMAIL PROTECTED]
Subject: RE: What happened to stream CopyTo?
I recently had a similar need... maybe this will help you?
http://www.windojitsu.com/code/asyncstreampump.cs.html
In my case, I wanted to tap the two streams together, asynchronously... so
I use async I/O (viz. Stream.BeginRead).
Heed the warning in the comments: although this technique will appear to
work on just any ol stream, if the source stream doesn't actually support
aync (eg: a FileStream opened with useasync=false) it will attempt to work
synchronously -- by looping recursively on your BeginRead calls! This can
easily overflow the stack. (That's bad.)
I put a check in there for the special case of FileStream.IsAsync -- but
sadly, there is no way to ask a generic Stream if it's going to spam your
stack if you call BeginRead.
MS, if you're listening: please consider promoting the IsAsync property up
to the Stream base class, ok? This whole "simulate async behavior by
feeding syncronous data, recursively" design is just awful -- I'm sure it
sounded nifty at the time, but it completely disrespects the non-blocking
semantics of BeginRead, and the potential for stack overflow is just
unacceptable.
Cheers,
-Shawn
http://www.windojitsu.com/
-----Original Message-----
From: Alex Ivanoff [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 19, 2004 15:05
Subject: What happened to stream CopyTo?
I want to copy one stream to another. In unmanaged world I would use
IStream::CopyTo. In .NET the only way I see is to do pairs of Read/Write.
What happened to CopyTo?
===================================
This list is hosted by DevelopMentorR http://www.develop.com
Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com
Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com