如何线程安全调用PictureBox.Image在C#中,目前给出3错误之一线程、目前、错误、安全

2023-09-06 14:35:13 作者:鱼忘七秒 人忘七年-

我使用的一种形式这个图片框,该图片框我用的是AForge code。我通过图片框的参考到我创建初始化的摄像头,并告诉它在哪里绘制它的帧摄像头类....所以它高兴地吸引它框...没有problemo。

但随后特定的时间(当我想要做的东西与所述图像,如果单击一个CHCK箱)...使用简单的code,我开始这个定时器:

  timer1.Enabled = TRUE;
 

此计时器的时间间隔设置为33。

所以,沿着每次经过循环,现在它的射击和我的code有这样的:

 私人无效timer1_Tick(对象发件人,EventArgs的)
{
    ...
    双值= detector.ProcessFrame(新位图(picCapture.Image));这里//错误
    ...

        TimerCallback TC =新TimerCallback(sendDataFast);
        System.Threading.Timer T =新System.Threading.Timer(TC,空,2000,Timeout.Infinite);

}
 

这行的上方有三种我看到的是错误的(堆栈跟踪如果有的话):

对象是目前在其他地方使用。

内存不足。 (在System.Drawing.dll程序的第一个机会异常类型System.OutOfMemoryException发生)

参数无效 (在System.Drawing.dll程序的第一个机会异常的类型'System.ArgumentException'的出现)

我敢肯定这些线程问题,但我不知道如何对付他们......我完全失去了。如果在该行的程序挂起上面,我通常可以点击再次在调试器中运行,一切都很好。但我不想马虎,只是把一个威利愿意不愿意尝试赶上仍在继续。我想弄清楚这个问题的根源。

我看到其他地方有人说,这可能是一个线程问题,并把这一行:     System.Diagnostics.Debug.Assert(!this.InvokeRequired,InvokeRequired);

所以,我没有在那个time1_click方法的顶部,但断言似乎并没有被发生的事情,但我不知道这是正确的地方为断言......是timer1_click在UI线程与否?

我现在怀疑我回顾ç的东西我初始化我的摄像头类的方式我$ C $:

或者说timer1_click中我也做一个调用此方法:

 无效sendDataFast(对象stateObject)
{
    EmergencyDelegate delEmergency =
           新EmergencyDelegate(mic.sendDataEmergency);

    //调用BeginInvoke的功能! // sendDataEmergency需要在一个画面图像picImage作为参数。
    delEmergency.BeginInvoke(picCapture.Image,NULL,NULL);
}
 
多线程面试题 线程安全

和完整性我这是怎么初始化我的摄像头类:

  = webcam的新网络摄像头();
        webcam.InitializeWebCam(REF picCapture,裁判picComparator,文献数据对象,这一点); //猜测这是调用线程问题
 

这是发生不立即发生这三个错误,似乎随机发生三者之一....使我认为它是一个线程的问题,但我还能怎么解决这个问题?创建委托出于某种原因,返回的双重价值,被称为如果需要调用是真的吗?

解决方案   

是timer1_click在UI线程或不?

取决于其计时器所使用。 sendDataFast绝对不是因为我们使用了System.Threading.Timer。

如果你看一看MSDN文档上System.Threading.Timer,你会看到下面的

  

System.Threading.Timer是一个简单的,   使用回调轻巧计时器   方法和由服务线程池   线程。不建议使用   使用Windows窗体,因为它的   回调不上的用户不会发生   界面线程。   System.Windows.Forms.Timer是一个更好的   选择与Windows Forms.choice用于Windows窗体。

这是解释了为什么你要冻结。

  

在执行的回调方法   计时器应重入的,因为它   被称为在线程池线程。该   回调可以被执行   同时在两个线程池   如果计时器的时间间隔小于螺纹   比执行所需的时间   回调,或者如果所有线程池   线程都在使用和回调   排队多次。

这意味着,如果你的函数失败下33毫秒的功能将被再次调用执行的手段。这大概就是这样,为什么你得到你所看到的例外。两个或多个线程试图使用相同的文件。你也可以有多个线程试图分配内存的大块和一个没有得到你所要求的大小的块。不知道为什么你得到的参数异常,但也可能是因为previous两种。

为此,我preFER的System.Timers.Timer的。它有一个自动复位属性设置为false。然后,在我的函数结束时,我打电话Timer.Start。你可以完成同样的事情与其他定时器,但它的一个小特里克。

下面是三个环节,你可能会发现有用

文章的定时器类的比较

在开始乔恩斯基特答案调用问题

埃里克利珀对什么是内存不足的异常可能是博客

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)

Parameter 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