I have an engine class that responsible to play streaming bytes with my Player class and write the bytes to file with my StreamWriter class.
I can have more than one engine and more than one players/writers.
Everything works fine until I need to close the file. when I say close I mean to add wave header and do some operation and then I dispose the writer and open a new writer to write the bytes.
My problem is that when the file is small its ok but when the file is larger (a few Mb) then the writer is null before I close the file. How can I avoid this?
Some code:
Engine class
AddBytesAndSave(byte,[] stream)
{
writer.AddAndSave(stream);
// here i think i must wait untill the operation complete
writer.Dispose;
writer = null;
}
Writer class
AddAndSave(byte[] stream)
{
writrToFileWorker.RunWorkerAsync(stream);
}
Obviously, or? When the Array is larger, it takes more time to physically write, and as you do not wait – well…
Technically the Problem is that it makes – from your code – ZERO SENSE to run the write async. Totally None. No real advice needed – any refactoring depends on circumstances we do not know from your code snippet. Just – if the write is asyn, then obviously the writer should not be closed in the AddBytesAndSave method.