Reputation: 2663
I have a simple WPF application in which I open a webcam and set it to 30 Frames per second, like this:
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
This application shows the camera preview in an ImageControl (30 frames per second).
Below the Image box, I've a TextBox
<TextBox Name="txtMsg" HorizontalAlignment="Left" Width="214" Height="22" Margin="0,260,0,0" TextWrapping="Wrap" VerticalAlignment="Top"></TextBox>
Now when I try to type in the textbox, it takes seconds to reply. Sometimes I press keys several times, but get no response. If I delay the camera capture event (e.g. 1 frame per second) it works fine.
My question is how can I have the UI updated frequently, but get a fast response in TextBox at the same time.
Thanks.
Upvotes: 0
Views: 221
Reputation: 5025
The webcam object is running in the UI Thread
. Here is an example, how you can start it in a separate Thread
. Be careful, because the ImageCaptured
event is fired in a background Thread
, so you have to call a Dispatcher
.
private void _StartWebCam()
{
ThreadStart webCamThreadStart = () =>
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
};
Thread threadnameThread = new Thread(webCamThreadStart) { IsBackground = true };
threadnameThread.Start();
}
private void webcam_ImageCaptured(object sender, EventArgs eventArgs)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
//Set you captured Image to your ImageControl
}));
}
Upvotes: 1