まず、技術的な知識が不足していて誤解を招く可能性があることをお詫びします。私は C# の初心者です。
いくつかの Web ページをスクレイピングして .png ファイルとして保存するプロジェクトを引き継ぎました。
private void CaptureWebPage(string URL, string filePath, ImageFormat format)
{
System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
web.ScrollBarsEnabled = false;
web.ScriptErrorsSuppressed = true;
web.Navigate(URL);
while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(5000);
int width = web.Document.Body.ScrollRectangle.Width;
width += width / 10;
width = width <= 300 ? 600 : width;
int height = web.Document.Body.ScrollRectangle.Height;
height += height / 10;
web.Width = width;
web.Height = height;
_bmp = new System.Drawing.Bitmap(width, height);
web.DrawToBitmap(_bmp, new System.Drawing.Rectangle(0, 0, width, height));
_bmp.Save(filePath, format);
_bmp.Dispose();
}
ただし、一部のページ (ごく一部) が原因で、プロセスがハングします。毎回というわけではありませんが、かなり頻繁です。問題はコードの次の部分にあるようです。
while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
web.ReadyState が「インタラクティブ」でスタックし、「完了」に進まないため、ループし続けるように見えます。
一定時間 web.ReadyState = 'Interactive' の場合、そのページのプロセスを再起動させるコードを挿入することは可能ですか? その場合、構文はどうなりますか?