tags:

views:

152

answers:

4

Hello everyone, I am using c# to integrate with a web cam. I need to generate a snapshot image every x miliseconds and save it to file. I already have the code up and running to save to file on a button click event, however I wonder what am I supposed to do when taking snapshots in the background - Should this be multi threaded? I'm honestly not sure. I could just block the UI thread, put Thread.Sleep and then just take the snapshot, but I don't know if this is right. I thought of using a background worker, but I am now experiencing cross threaded difficulties with SendMessage... So I wonder if I should even go and bother to multi-thread or just block the UI.

Help greatly appertained, thanks in advance.

A: 

Maybe this will help.

bitbonk
I see that it's not multi threaded. So I assume I shouldn't bother and just leave it as it (=not multi threaded)?
Rita
+1  A: 

There will be a physical hardware limit to how fast the camera can update its pixel buffer. Webcams don't go far above 30fps. Getting the actual image should be more or less instantaneous (unless at very high res), so you would not require threading to start off with. When I did it a while ago I used the approach as given on

http://weblogs.asp.net/nleghari/pages/webcam.aspx

filip-fku
A: 

I think you should put this task on a separate thread. The process of creating and saving the image may take more time is some situations and at that time your HMI may freeze. To avoid this put this task on a separate thread.

Ram
A: 

You could create a timer to kick a delegate every n milliseconds and that delegate could queue a worker thread to do what your OnClick() handler does already.

I would NOT write this as a single-threaded app because, depending on the performance of the user's webcam, you could easily end up in an eternal loop handling timer events, causing your main UI thread to be permanently blocked.

ThreadQueue.QueueUserWorkitem((args) => 
{
    // Blah ...
}

should not require much effort to get working correctly.

bitcrazed