Here's how I usually implement this problem on the full version of .NET on the PC: use one thread for the GPS data, one for the sensor data, and a third thread to send the data to the SD card. You could use a Queue object to manage the data flow. Your worker threads would stuff the data into the queue, using a locking object to eliminate collisions. I haven't used this kind of thing on the NetMF however - there may be threading idiosyncrasies on NetMF for which I haven't accounted.
object lockObject = new Object();Queue queue = new queue();AutoResetEvent dataIsAvailable = new AutoResetEvent(false);public static void Main(){ ... Create Logging Thread (Make sure to start this one first) ... Create GPS Thread ... Create Sensor Thread Thread.Sleep(Timeout.Infinite);}public void GPSThreadStart(){ while (true) { .... raw GPS data received ... create data object from raw GPS data lock (lockObject) { queue.Enqueue(data); } dataIsAvailable.Set(); }}(Sensor thread is the same basic idea)public void LoggingThreadStart(){ while ( true ) { dataIsAvailable.WaitOne(); while (queue.Count > 0 ) { object data; lock (lockObject) { data = queue.DeQueue(); } ... Write data to SD card } }}
The AutoResetEvent allows the threads to wait until data is available without using any CPU cycles. The lock() statements make sure the Dequeue/Enqueue calls are completed before a thread context switch is allowed, avoiding collisions. Using this scheme, you can enqueue sensor data inside interrupt handlers, and even write the logging data to the same file if you like. When I do that, I usually use a base class to enforce a common record format, then derived classes for each different type of record.
Hope that helps,
Eric
- NeonMika / Markus VV. likes this