I am using AnalogMultiChannelReader to do asynchronous AI.
The AI thread runs independently of my UI.
It uses a thread-safe queue to send acquired data to a few UI controls.
The AI code resides in my UI class.
There is a separate class for my file IO.
The fileIO simply writes acquired data to a Binary or CSV file.
An instance of the FILE io class is created in the UI class.
The file writing is executed on the AI thread.
What is the best way to get the file writing
on a thread separate from the Asynchronous AI?
my sample rates vary depending on the job
..up to 20 or 30 Ksamples/s /ch
Before you go down the route of another thread and another queue, i would recommend you try and benchmark using file i/o synchronous writes from the UI thread and see if you notice any UI slugishness. You're probably streaming data to a disk and I'm sure the disk can keep up with the rate you are going at.
You could consider asyncrhonous I/O, but that might not give you any advantage. If you are writing small data chunks often, you might end up hogging the thread pool because of the high number of I/O requests pending, not to mention the possibility of data being written out of order. See this link for more info. So it might cause more harm than good.
I guess you could also go for synchronous file i/o in the AI thread, but the choice between using the UI and the AI thread would affect how you might handle file error handling (hard disk full, file not found, stuff like that). You might be able to better handle those conditions in the UI thread by buffering up the data until the error is dealt with (writing to the event log, backing up to a db), but thats just something to consider.
this is what you mean by "synchronous in the AI thread" ?
my UI thread has a form timer that dequeues a queue for UI controls (graph, gauge, etc..)
i am guessing that writes from the UI thread
would only be reasonable if my AI was not on a separate thread..
seems like the data coming in on the AI thread would
not be synched with the UI timer...dangerous or impossible?
i started this post because when I press my "stop recording to file" button,
the AI thread hogs resources and my UI thread stops for a few seconds while the AI thread finishes writing.
I meant synchronous file i/o in the AI thread, as opposed to async file i/o. instead of the filestream class scheduling the write in a worker thread (using BeginWrite), it does it synchronously (using Write) and doesnt return until the data is written to file.
The AI callbacks will be firing at their own rate independant of the timer. If you were doing the writes in the UI timer callbacks (and hence in the UI thread) you would be grabbing data from the queue. This is the same data that is being displayed anyway. Since you are reading from the queue is responsible for making sure that the data being returned by the AI operation is in order. Ultimately things would sync up as you empty out the queue.
But Im not sure why the AI thread is hogging resources when you hit the stop button. Im assuming that you dispose the task when you hit the "stop data to file" button. Did this happen after you added the file I/O to the AI async callback? Are you closing the file stream when you stop recording? If its happening becuase of the file I/O, you could consider flushing the filestream every so often so as to prevent a buffer build up at the end.