Home Ask Login Register

Developers Planet

Your answer is one click away!

Curb February 2016

asynchronous beginwrite endwrite Unity (net 2.0)

I'm trying to figure out the proper way to write some bytes (in this case a recorded .wav file) to harddisk in an asynchronous way.

I'm using unity so as far as I know I'm limited to using Filestream.BeginWrite and EndWrite.

What I got so far seems to be working, but I'm worried that, because of my lack of experience with asynchronous calls I'm not properly closing the filestream after it's done, leaking memory.

I'm starting the BeginWrite like so:

object t = null;

fileStream.BeginWrite(bytesData, 0, bytesData.Length, new  AsyncCallback(EndWriteCallback),  t); 

Then I've got an EndWriteCallback that has this:

public static void EndWriteCallback(IAsyncResult e){
    FileStream fs = e.AsyncState as FileStream;

Is this enough? Again, it seems to be working alright, but I feel like I'll have to call FileStream.Close() somewhere.

Thanks in advance


andeart February 2016

As @ChristophKn mentioned in the comment above, fs.dispose() is probably the best way to release the resources.

Post Status

Asked in February 2016
Viewed 1,275 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life