Do Events causing overflow
-
I’m writing a C# DLL that executes a web browser. I have to grab a static image when the browser is done updating. So I wrote the following code.
public string SaveImageToFile()
{
int done = 0;
String file = "";
while (done < 20)
{
if (((webbrowser1.IsBusy == false) && (webbrowser1.ReadyState == WebBrowserReadyState.Complete)) || (done == 19))
{
Bitmap docImage = new Bitmap(webbrowser1.Width, webbrowser1.Height);
webbrowser1.DrawToBitmap(docImage, new Rectangle(webbrowser1.Location.X,
webbrowser1.Location.Y, webbrowser1.Width, webbrowser1.Height));
file = "C:\\Rumper\\tempA" + num + ".bmp";
docImage.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
++num;
done = 20;
}
else
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
++done;
}
}
return file;
}However when I call SaveImageToFile from my MFC application, I get a stackoverflow. If I comment out the DoEvents. No stack overflow and no image. I need to replace DoEvents, but I’m not sure what would be a better method. Thanks
Programmer Glenn Earl Graham Austin, TX
-
I’m writing a C# DLL that executes a web browser. I have to grab a static image when the browser is done updating. So I wrote the following code.
public string SaveImageToFile()
{
int done = 0;
String file = "";
while (done < 20)
{
if (((webbrowser1.IsBusy == false) && (webbrowser1.ReadyState == WebBrowserReadyState.Complete)) || (done == 19))
{
Bitmap docImage = new Bitmap(webbrowser1.Width, webbrowser1.Height);
webbrowser1.DrawToBitmap(docImage, new Rectangle(webbrowser1.Location.X,
webbrowser1.Location.Y, webbrowser1.Width, webbrowser1.Height));
file = "C:\\Rumper\\tempA" + num + ".bmp";
docImage.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
++num;
done = 20;
}
else
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
++done;
}
}
return file;
}However when I call SaveImageToFile from my MFC application, I get a stackoverflow. If I comment out the DoEvents. No stack overflow and no image. I need to replace DoEvents, but I’m not sure what would be a better method. Thanks
Programmer Glenn Earl Graham Austin, TX
Is this method called from within an event handler? If so, calling DoEvents may be refiring the same event and calling your code again, and again while you are still inside of the While loop. I would suggest removing the event handler at the begining to the SaveImageToFile method, then adding the handler at the end. This can also be done within the Event handler method as well. I have had way to many headaches for this problem to hit me again, and 99% of the time, it's because the event is being fired before your code completes. Hope this helps, Scott Page
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem." ( President Ronald Reagan)
-
Is this method called from within an event handler? If so, calling DoEvents may be refiring the same event and calling your code again, and again while you are still inside of the While loop. I would suggest removing the event handler at the begining to the SaveImageToFile method, then adding the handler at the end. This can also be done within the Event handler method as well. I have had way to many headaches for this problem to hit me again, and 99% of the time, it's because the event is being fired before your code completes. Hope this helps, Scott Page
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem." ( President Ronald Reagan)
No I’m not calling from an event but there are several timer procs in the main MFC software. I have totally abandoned the DoEvent method. It causes to much strange stuff. Now I have to keep my webbrowser control going. I need it to paint the info. Thanks for thring to help
Programmer Glenn Earl Graham Austin, TX