我的 PictureBox 电话中有另一个问题,给了我 3 种错误,特别是来自Conrad Frix的一些很好的答案。所以它让我弄清楚我的问题出在哪里,但现在要解决它我不是 100% 确定的。
基本上我有一个 Windows 窗体计时器,它正在检查某个事件是否为真,如果是,那么它会告诉系统在所述事件(一个值)超过某个阈值后 2 秒发送一些数据。
我认为我拥有的所有计时器都在用我的 PictureBox 创建一个令人讨厌的竞争条件,我在几个地方使用它来获取图像:
new Bitmap(myPicBox.Image);
ETC...
我在某处读到计时器的间隔应该至少为 50。从 33 开始设置。我发现我可以做一个 picCapture.InvokeRequired 来看看它是否会基本上死掉。我知道我需要使用一个委托,但只使用它们来设置一些东西......而不是从中获取图像......不知道如何设置它......我知道确实是什么原因造成的,就是这个代码组合:
private void timer1_Tick(object sender, EventArgs e)
{
if(someCOnditionTrue)
{
TimerCallback tc = new TimerCallback(sendDataFast); //only
//doing all this so i can have the method run two seconds after
// the condition is detected to be true.
System.Threading.Timer t = new System.Threading.Timer(tc, null, 2000, Timeout.Infinite);
}
}
void sendDataFast(Object stateObject)
{
//using this so the execution is not haulted while the sending of data takes place.
EmergencyDelegate delEmergency =
new EmergencyDelegate(mic.sendEmergencyData);
Image imgclone;
if (picCapture.InvokeRequired)
{
Console.WriteLine("HFS Batman! its going to die ");
}
lock (lockObject2) //i admit no clue what im doing here and doesn't seem to help.
{
Image img = picCapture.Image;
imgclone = (Image)img.Clone();
}
delEmergency.BeginInvoke(imgclone, null, null); //deep in the call to
//sendEmergencyData i get the **ParameterNotValid** almost everytime.
imgclone.Dispose(); //to free memory?
}
根据我之前的问题,在 timer1_tick 事件中似乎不再出现内存问题或其他错误......(内存不足错误是一个)。
我认为最大的问题是当我需要它的图像数据时如何处理 picCapture.InvokeRequired ?我确定它在 timer1_click 中的线程计时器调用导致了这个....