0

I have to use a camera SDK for a microscope that comes with a sample C# Windows Forms application. The SDK functions are imported to the app via DLLImport. In this sample application the single frames from the camera are read from RAM via a callback function. This callback function then updates a PictureBox to show the live image.

For my useage however I want to not only show the frames, but also record them. I think the easiest way to do this is by using SharpAvi. However I don't know how to initialize the AviWriter (I obviously can't initialize it everytime the callback fires) and then add the single frames to the stream from the callback function.

At the moment I solved my problem by using another function that waits in a seperate thread for a public counter to go up - the counter itself is updated in the callback function. Everytime the counter is updated this function then also reads the frame from RAM and adds it to the AviWriterStream. To top it of I had to make the thread block my UI by using

var resetEvent = new ManualResetEvent(false);

System.Threading.ThreadPool.QueueUserWorkItem(delegate
{

    WriteAVI()
    resetEvent.Set();

}, null);

resetEvent.WaitOne(); 

This is due to the fact that I need to record several videos at different XY positions - so everytime my for loop arrives at a new XY position the recording is triggered and I have to make sure to only move to the next position once the recording is actually finished.

My way of doing this is obviously hackish and very bad practice (I already feel bad but just don't know better) and I am wondering what the correct solution would be. So far it works - just the UI freeze is really annoying.

Edit: These are probably two questions: 1) How to correctly write frames to the AVI and 2) How to wait for the recording to be completed without making it freeze my UI

Daniel M
  • 105
  • 2
  • 11
  • Complicated question, but from the source code you start a thread (`QueueUserWorkItem`) and in the next line, you wait until it completes (`resetEvent.WaitOne()`). Why did you put it in a separate thread, if after starting it you already wait for it to complete in the next line? To me, it looks like `resetEvent.WaitOne();` is not needed. – Sjips Nov 07 '14 at 11:20
  • Because without the separate thread I would not be able to wait for the counter to update and add the next frame to the AVI. However without waiting for the thread to be completed my for-loop would just go straight to all the XY positions one after the other, starting new recordings without the first one being completed. – Daniel M Nov 07 '14 at 11:24
  • That makes sense. Why not wait in the thread for the update for the counter? `resetEvent.WaitOne();` will block your UI code, so you need to find a way to get rid of it. – Sjips Nov 07 '14 at 11:25
  • I don't understand what you mean by that. I am waiting for the update of the counter in the separate thread (by using a while(counter < MaxNumberOfFrames) statement. – Daniel M Nov 07 '14 at 11:29
  • Oh, the `while` was not in the posted code. But if you put your `while...` code just before `WriteAvi();` in the thread code that you posted, how about that? – Sjips Nov 07 '14 at 12:15
  • That was because the while statement is inside the WriteAvi() - and why would it help to put the while before the WriteAvi? Then the entire code would not work as the while statement would catch the framecounter and the actual writeAvi would be started after all the frames have been lost. – Daniel M Nov 07 '14 at 12:28
  • Okay, It looks like I'm getting the point. `WriteAvi()` writes the frames by means of a framecounter; the whole process would normally take too long so you execute it in a separate thread. From your question, I was assuming that the framecounter was incremented in a callback function. Would it make sense to post the `WriteAvi()` code? – Sjips Nov 07 '14 at 13:38
  • The whole process does not take too long - the separate thread is necessary as otherwise the wait via while would block the UI just as well. I solved the problem by putting the calling method into a separate thread as well - this way the UI stays respnsive. Basically I have two separate threads now: one is the initial WriteAvi which grabs the frames and the other consists of the for loop to go to the XY positions and the call to start recording. – Daniel M Nov 07 '14 at 13:58

0 Answers0