开发者

How to thread safe call to PictureBox.Image in c# , currently gives one of 3 errors

开发者 https://www.devze.com 2023-01-16 07:22 出处:网络
I am using this PictureBox on a form, to this picture box I use the AForge Code.I pass the REFERENCE of the pictureBox into a webcam class I create that initializes the webcam and tells it where to dr

I am using this PictureBox on a form, to this picture box I use the AForge Code. I pass the REFERENCE of the pictureBox into a webcam class I create that initializes the webcam and tells it where to draw its frames to....so it happily draws it frames... no problemo.

But then certain times (when I want to do stuff with said image, if a chck box is clicked)...I start this timer using simple code:

timer1.Enabled = true;

this timer's interval is set to 33.

So now its firing along and every time through the loop my code has this:

private void timer1_Tick(object sender, EventArgs e)
{
    ...
    double value = detector.ProcessFrame(new Bitmap(picCapture.Image)); //errors here
    ...

        TimerCallback tc = new TimerCallback(sendDataFast);
        System.Threading.Timer t = new System.Threading.Timer(tc, null, 2000, Timeout.Infinite);

}

That line above has one of three errors I have seen on it (Stack traces where available):

Object is currently in use elsewhere.

Out of Memory. (A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll)

Par开发者_开发知识库ameter not valid (A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll)

I am certain these are threading issues but I have no clue how to deal with them...I am totally lost. If the program hangs on that line above, I can usually click run again in the debugger and all is well. But I don't want to be sloppy and just put in a willy nilly try catch that continues. I would like to figure out the root of this issue..

I saw somewhere else someone said it could be a threading issue and to put this line: System.Diagnostics.Debug.Assert(!this.InvokeRequired, "InvokeRequired");

So I did at the top of that time1_click method but the assert doesn't seem to be happening, but i am not sure this was the right place for the assert... is timer1_click in a UI thread or not?

I suspect now that I reviewed my code its something with the way I initialize my webcam class:

Or within that timer1_click I also make a call to this method:

void sendDataFast(Object stateObject)
{
    EmergencyDelegate delEmergency =
           new EmergencyDelegate(mic.sendDataEmergency);

    // call the BeginInvoke function! //sendDataEmergency takes in a picture Image picImage as an argument.
    delEmergency.BeginInvoke(picCapture.Image, null, null);
}

And for completeness this is how I initialize my webcam class:

        webcam = new WebCam();
        webcam.InitializeWebCam(ref picCapture, ref picComparator, ref dataObject, this);            //guessing this is calling threading issues        

Those three errors that happen don't happen right away, seems to happen randomly one of the three.... leads me to think its a threading issue but how else can I fix this? creating a delegate for some reason that returns that double value and is called if invoke required is true?


is timer1_click in a UI thread or not?

Depends on which timer you are using. sendDataFast definitely isn't because you used a System.Threading.Timer.

If you take a look at the MSDN documentation on System.Threading.Timer you'll see the following

System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms.choice for use with Windows Forms.

This is explains why you're getting freezes.

The callback method executed by the timer should be reentrant, because it is called on ThreadPool threads. The callback can be executed simultaneously on two thread pool threads if the timer interval is less than the time required to execute the callback, or if all thread pool threads are in use and the callback is queued multiple times.

Which means if your function fails to execute in under 33 ms the function will be called again. This is probably the case and why you're getting the exceptions you're seeing. Two or more threads are trying to use the same file. You may also have multiple threads trying to allocate a large block of Memory and one fails to get the block of the size you've requested. Not sure why you're getting the argument exception but it may be because of the previous two.

For this reason I prefer the System.Timers.Timer. It has an AutoReset Property that set false. Then at the end of my function I call Timer.Start. You can accomplish the same thing with the other timers but its a little tricker.

Here are three links you might find useful

Article on Comparison of the Timer Classes

Jon Skeet Answer on a Begin Invoke Question

Eric Lippert Blog on what an OutOfMemory Exception likely is


I think while seeing this line

double value = detector.ProcessFrame(new Bitmap(picCapture.Image)); 

you are trying to modify the picCapture.Image which is a Picturebox image and you are doing this every 33 millisecs.

1st What this detector.ProcessFrame do?

2- You should pass the actual image uri to the New Bitmap rather than using a Image which is the source of PictureBox

3- Why are you creating more timers in tick event ????


For OutOfMemoryException, I would suggest replacing

double value = detector.ProcessFrame(new Bitmap(picCapture.Image));

with

double value;
using(Bitmap bmp = new Bitmap(picCapture.Image)) {
    value = detector.ProcessFrame(bmp);
}

so your temporary Bitmap will be disposed as it should be.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号