G120 Unstable behaviour after "_fs = new FileStream(...)"

Having an application that runs fine on EMX (Spider) + MF 4.2

I have ported the application to a custom board with G120 but experience problems like reboot and hangs

I was able to isolate when the board “hangs and reboot” its in this code

_fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 262144);

It is not always on the same file that is opened. The files exist on an uSD card. I have tried different SD card brands, although they all work fine on the Spider

When i am debugging the application and after the crash i try to stop debugging and in more that 80% my development system with win7 crashes too (BOD) the other 20% VS2010 returns to normal edit mode.

When win7 does not crash and i can start a new debug session, sometimes i am able to reach the main method sometimes the system stops after loading the assemblies and perfoming a GC: performing heap compaction. see thread “http://www.tinyclr.com/forum/topic?id=10185&page=1

Whem i am able to step in to Main() based on debug info printed i can see that ExtendedWeekReference data is gone.
Also the RS9110 module will update its firmware by itself after a call to WIFIRS9110.Open (Which after 100 second fails)

Anyone a suggestion?

Can you try USB instead of USB?
Can you provide a test app we can try on our end?
Did you try other sd cards?

USB instead of USB???

Yes, tried different SD cards

I will try to create an app tomorrow…

Instead of sd

@ RobvanSchelven -

I follow the link you provided on your previous post…

I also use SD Card on G120, and have no problem using it ! I really engage you to have a look on the PersistentStorage part of your code and the scope of the variable u use a each part of your code, and use the Premium.IO lib also.

I’m going to post the SDFileSystem Class I use on codeshare and give you a link on it when done… Maybe it will help you…

I’m using a SD module on G120HDR without issue as well.

Thanks guys, i will continue debugging and testing…

Here’s the link :

You can use it as it is to check if it’s ok as it is for me :


Thanks for sharing, but its not so easy to replace my part with yours, since i added some caching/sharing to be able to stream large media files…

What i just discovered is that the file creation/modification date on the SD card has changed to the date of last access to that file, while i am sure i only do read and obtain file info… Not sure yet why that happens.

@ RobvanSchelven -

Nevermind on replacing, but yu can just built a new simple project with this class to see if you have the same problems with the SD…

As you like !

@ Gus, my board does not have a USB host port… I ordered a G120HDR today to make sure its not a hardware issue from my side and to be able to test with a wired network instead of wifi…

What we need to know if this is SD problem of FAT problem.

Ok… hopefully the board arrives on Monday from mouser…

We are ready to help on our end if you give us some test code.

Did some testing with G120 and SD card. I noticed an annoying difference between MF 4.1 & 4.2

Accessing a file as in the example below modifies the file creation date, while there is no any write action in the code. It makes actions like file synchronization impossible.
Although theoretically a write to flash has impact on its durability. Not really desirable if only read actions are necessary.

using System;
using System.IO;
using System.Threading;
using GHI.Premium.Hardware;
using GHI.Premium.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace G120test
    public class Program
        private static PersistentStorage _pse;
        private static FileStream _fs;
        private static DateTime _lastModifiedDate;
        private static int _contentLength;

        private static readonly OutputPort StatusLed = new OutputPort(G120.Pin.P1_15, false);

        public static void Main()
            var dt = new DateTime(2013, 1, 1, 0, 0, 0);

            if (PersistentStorage.DetectSDCard())
                    _pse = new PersistentStorage("SD");
                    SystemError("Can't create persistentStorage");
                SystemError("No SD Card");

            for (int i = 0; i < 1000; i++)
                var fi = new FileInfo(@ "SD\Video\MLinePromo.mp4");
                _lastModifiedDate = fi.CreationTimeUtc;

                    _fs = new FileStream(@ "SD\Video\MLinePromo.mp4", FileMode.Open, FileAccess.Read, FileShare.Read);
                catch (Exception e)
                    SystemError(@ "Can't open SD\Video\MLinePromo.mp4");

                _contentLength = (int) fi.Length;

                var buffer = new byte[32768];

                int blockCnt = _contentLength/32768;
                for (int b = 0; b < blockCnt; b++)
                        _fs.Read(buffer, 0, 32768);
                    catch (Exception)
                        SystemError("Error reading file");

                Debug.Print("n=" + i + " " + DateTime.Now.ToString());


        private static void SystemError(string msg)
            while (true)

@ Rob

I’ll try your code as is tomorrow and tell back if I see the same issue…

@ LouisCpro… Thanks would be great…

Ok After checking, I have the same issues as Rob. It seems like if the file was recreated after any kind of access trying…

@ Gus, I tried to have a look in NETMF PK 4.2 Code, but my C++ is so far from me that I did not succeeed, even going thru the Native_IO level of he framework… :wall:

If you find something, do not hesitate because effectively, it sound like a problem if we want o synchronize files with “last modified” or “last created” rules…

@ LouisCpro Thanks for testing!!

Also an issue for web servers who can’t reply properly to an “If-Modified-Since” ‘request’

@ GUS, compared today my application running at the custom board and running at the G120HDR. The application is basically a web server based on sockets.

There is no difference in behavior. both are unstable. Beside the SD card problem as described above the application runs fine when the RS9110 module is replaced with an ENC28 module… (repeat that the application runs fine with RS9110 in combination with a spider). It really looks like that the problem is related to wifi and sockets on G120… since there’s no exception but simply a lock-up and or reboot it is hard to tell where things go wrong.

A difference i noticed is that the files access from SD card by the web server get less modified (creation date) when the board runs with ENC28…

Not sure if this is by design, but if a G120 module has run with RS9110 and want to replace it by an ENC28 the boards needs to be flashed again otherwise it will not work.
a NetworkInterfaceExtension.AssignNetworkingStackTo(…) throw an exception saying that … is already assigned or so…

Please give me your thoughts on how to continue.
Thanks, Rob