Main Site Documentation

Stream Close VS Dispose & TRY/Catch blocks with G400


#1

I just had a minor meltdown (literally needed a crowbar to get in) after a g400 locked up, and trying to figure out the mistake in the code. 90% of it is re-used from another project, which has several devices in service for the last year or so.

That said, normally I just use this block of code,


sdCard.StorageDevice.WriteFile(fname, rawControl);
sdCard.StorageDevice.Volume.FlushAll();

but, I added a logging portion, this is where I suspect my error is, although I did some searching, and did find that calling “Close”, calls Dispose in Stream.cs (but it also mentions to moving towards I-Disposable, although I will admit to not understanding what that means)


Debug.GC(true);

byte[] rawBytes = System.Text.UTF8Encoding.UTF8.GetBytes(resp); //"" + DateTime.Now.ToString());
Stream rawStream = sdCard.StorageDevice.OpenWrite("\\SD\\File" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + ".BIN");
 rawStream.Seek(rawStream.Length, 0);
 rawStream.Write(rawBytes, 0, rawBytes.Length);
 rawStream.Flush();
 rawStream.Close();

but have found references to both delegates, weak delegates, and items using “I-disposable”

From another post online,
“It is recommended to use the using pattern when dealing with anything that implements IDisposable”

In writing this post, I also found that the SDCard.StorageDevice Write functions encapsulate the writes in a “USING” block, so where I thought the TRY/Catch block might be responsible for the disposing, I think the actual Write function may be responsible.

So, my questions are,

Do I need to Call Dispose?
Does a “Try/Catch” Block, force disposing?
Do I need to change to using a “USING” Block?
How do I know when to use “USING” in the future?
How do I know if I ever need to use “Dispose” in the future?

Thanks
Michael.