multiple threads for multiple cameras  
Author Message
Micka10





PostPosted: .NET Base Class Library, multiple threads for multiple cameras Top

Hi!


I have a problem with a form that has to display the images taken from three cameras. I'm using three Axis IP cams, that provide the images to te PictureBoxes, and each PictureBox is being in the separated thread.

public void PutImagePictureBox1()
{
Thread pictureBox1Thread = new Thread(new ThreadStart(SafeImageLoad1));
pictureBox1Thread.Name = "PictureBox1 Thread";
pictureBox1Thread.IsBackground = true;
pictureBox1Thread.Start();
}

To refresh the PictureBoxes, I'm using Invoke.

public void SafeImageLoad1()
{
while (camera1_.bInuse == true)
{
try
{
image1 = camera1_.GetImage();
ImageLoad1(image1);
}
catch (ThreadAbortException exc)
{
Console.WriteLine("Refresh the picture box 1 exit!");
}
}
}

void ImageLoad1(Bitmap myImage)
{

if (pictureBox1.InvokeRequired)
{
SetImageCallback x = new SetImageCallback(ImageLoad1);
pictureBox1.Invoke(x, new object[] { myImage });
}
else
{
pictureBox1.Image = myImage;
pictureBox1.Refresh();
}
Thread.Sleep(300);
}

And here is the problem: after few seconds my form stops responding. I don't get any exception, but it looks that there is some problem with calling this threads.

Could you please help me with this

Thanks!


.NET Development20  
 
 
RizwanSharp





PostPosted: .NET Base Class Library, multiple threads for multiple cameras Top

First of all, If you are using .Net 2.0 Better use 3 BackgroundWorker components than using Threads (More Efficient, Response, Structural and Recomended way).

Second, Without seeing all your code no one can comment on this code,

  1. You are Creating Thread in PutImagePictureBox1, It has scope limited to that function only. I dont know how your code is running like this.
  2. You have not metioned the Code where you are Calling SafeImageLoad1() and ImageLoad1.

May be the problem is in other code!

Plus if you still want to use the Threads instead of BackgroundWorker then atleast declare instances of Threads at class level so it can have broader scope.

Best Regards,



 
 
Greg Beech





PostPosted: .NET Base Class Library, multiple threads for multiple cameras Top

Just to clarify:

1. Threads will continue to run until they are complete, irrespective of whether you still have a reference to them. It may be better practice to keep a reference if you need to but it isn't necessary.

2. SafeImageLoad1 is the target of the delegate used to start the thread, so it will be called when the thread starts.

I can't see anything inherently wrong in the code from a threading aspect but I'm not a winforms guy so there may be something wrong with that aspect of it. You could use a BackgroundWorker but I don't see anything inappropriate about manually creating threads in this instance. What is your CPU utilisation like It could be that you are just burning all the available CPU. What happens if you use a longer sleep between updates



 
 
nobugz





PostPosted: .NET Base Class Library, multiple threads for multiple cameras Top

Your Thread.Sleep() call is not kosher. You'll execute it twice for each image, once on the camera thread, again on the GUI thread. In effect, your GUI thread is doing a lot of sleeping during which it is not servicing any invoke callbacks. See what happens when you move the Sleep() call into the if (pictureBox1.InvokeRequired) branch...


 
 
Micka10





PostPosted: .NET Base Class Library, multiple threads for multiple cameras Top

Hi nobugz,

It looks that was the problem. I moved Sleep() to (pictureBox1.InvokeRequired) branch as you suggested, and ow it works perfectly.

Thanks a lot!