I managed to get a webcam running using Aforge. The code can be found in my answer here.
Now I want to do some computer vision stuff and return back some results to the main form. The threading problem I have is in this part from that code:
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{ Bitmap video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = video;
// count red colors
// not yet written
TextBox1.text = "demo error"; // >> i like to return a value to the main form
}
The problem however is it’s doing the video frames in another thread, and now I can’t return answers from that thread to the main form back. I didn’t write the Aforge, I only linked it and made it work on my form.
I can’t change the way how Aforge video works. I mean it was never made to result something back that’s something I like to do. Are there ways to get a result back, like a string text in a textbox. On the main form where this code runs from?
I am able to follow a sample code of threading but this is a bit over my head since I got not much influence on how video acquisition in Aforge was designed.
I have been wondering if raising another thread just to update a textbox might work but I am not that sure and wonder that in the end one would create to many threads, causing to much kernel switching load.
Some additional info: the code thread above is started with this button function below.
private void button1_Click(object sender, EventArgs e)
{ FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
Try writing your code like this:
The
.Invokepushes the execution of theActiondelegate onto the same thread that created thepictureBox1control, in order words, the UI thread.