WebBrowser发出错误

时间:2017-06-28 07:19:05

标签: c# asp.net-mvc hangfire

我使用hangfire每3分钟更新一次屏幕截图。获取屏幕截图有多个URL

为了更新截图,我正在使用WebBrowser

我的篝火代码如下:

public ActionResult Recurring()
{
  RecurringJob.AddOrUpdate(() => JobExicute(), Cron.MinuteInterval(3));
}
public void JobExicute()
{
Thread _t1 = new Thread(new ThreadStart(MPLS1));
            _t1.SetApartmentState(ApartmentState.STA);
            _t1.Start();
            _t1.Join();

            Thread _t2 = new Thread(MPLS2);
            _t2.SetApartmentState(ApartmentState.STA);
            _t2.Start();

            Thread _t3 = new Thread(MPLS3);
            _t3.SetApartmentState(ApartmentState.STA);
            _t3.Start();

}

public static void MPLS1()
        {

            Logger.Log("Services is strated on " + DateTime.Now + "", 8);

            try
            {
                using (WebBrowser Browser = new WebBrowser())
                {
                    Browser.ScrollBarsEnabled = false;
                    Browser.ScriptErrorsSuppressed = true;
                    //sw.WriteLine("On WebBrowser");

                    DateTime start = DateTime.Now;

                    Browser.Navigate("http://myURL1 ");
                    while (Browser.ReadyState != WebBrowserReadyState.Complete || start > DateTime.Now.AddSeconds(-30))
                    {
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(100);
                    }
                    //Browser.Width=Browser.Document.Body.ScrollRectangle.Width;
                    //Browser.Height=Browser.Document.Body.ScrollRectangle.Height;

                    Browser.Width = 4000;
                    Browser.Height = 3000;//Browser.Document.Body.ScrollRectangle.Height;


                    using (Bitmap bitmap = new Bitmap(3000, 2000))
                    {
                        string MapPath = ConfigurationManager.AppSettings["MapPath"].ToString();
                        Browser.DrawToBitmap(bitmap, new Rectangle(0, 0, 3000, 2000));
                        Image img = bitmap;
                        bitmap.Save(@"" + MapPath + "screeshot1.bmp", ImageFormat.Png);

                    }
                    //sw.WriteLine("Services is completed on " + DateTime.Now + "");

                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error Message: " + ex.Message, 8);
                Logger.Log("Stack Trace: " + ex.StackTrace, 8);
            }
            finally
            {
                Logger.Log("Services is End on " + DateTime.Now + "", 8);
            }
        }

与MPLS2相同,MPLS3与MPLS1相同但URL不同。
但是在日志文件中我收到的错误如下:

  

错误消息:错误HRESULT已从调用COM组件返回E_FAIL。   堆栈跟踪:在System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(对象和URL,对象和标志,对象& targetFrameName,对象&postData,对象&标题)      at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL,Object& flags,Object& targetFrameName,Object& postData,Object& headers)      在System.Windows.Forms.WebBrowser.Navigate(String urlString)      在HangfireCronJobs.Controllers.HomeController.MPLS1()


请给我更好的解决方案。
感谢您的推进。

0 个答案:

没有答案