using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AIV_BLL;
using AIV_DAL;
using AIV_UI;
using System.IO;
using System.Data;
using AIV_Common;
using System.Threading;
using System.Configuration;
using System.Windows.Forms;
using AIV_UI.WebSiteSearch;namespace AIV_UI.QuickWin
{
    class clsESLErrorExtract_Thread
    {
        #region form
        frmESLErrorExtract frm;
        #endregion
        #region 属性
        private Thread extractThread;        //确定是否要重新开始加载的计数变量
        static int beginTime = 0;        //5秒钟检查一次网页是否加载完成的Timer控件
        System.Timers.Timer cpTimer = new System.Timers.Timer();        //根据登录页面传入的重新开始查找时间监控等待时间的Timer控件
        System.Timers.Timer againTimer = new System.Timers.Timer();        //取得设定的重新开始查找的计时时间
        private int RTime = Convert.ToInt32(ConfigurationManager.AppSettings["ResporceTime"].ToString());        //当前日期
        string strCurrentDate = string.Empty;
        
        String strUserId = Utility.GlobalUserId;        #endregion         #region 对象参数
        //数据DownLoad中过程显示webbrowser
        private System.Windows.Forms.WebBrowser wbESL;
        public System.Windows.Forms.WebBrowser WbESL
        {
            get { return wbESL; }
            set { wbESL = value; }
        }        //数据导成EXCEL进度条
        private System.Windows.Forms.ProgressBar extractProgress;
        public System.Windows.Forms.ProgressBar ExtractProgress
        {
            get { return extractProgress; }
            set { extractProgress = value; }
        }
        #endregion
        #region  线程控制方法
        internal void Start()
        {
            extractThread = new Thread(DoExtract);
            extractThread.IsBackground = true;            extractThread.Start();
        }        internal void Stop()
        {
            if (extractThread != null)
            {
                extractThread.Abort();
                extractThread = null;
            }
        }
        #endregion
       
        private void DoExtract()
        {
            SearchData();
            //5秒钟检查一次网页是否加载完成的Timer控件
            cpTimer.Interval = 5000;
            cpTimer.Elapsed += new System.Timers.ElapsedEventHandler(cpTimer_Tick);            //根据登录页面传入的重新开始查找时间监控等待时间的Timer控件
            againTimer.Interval = 1000;
            againTimer.Elapsed += new System.Timers.ElapsedEventHandler(againTimer_Tick);            //得到当前日期时间,精确到毫秒
            strCurrentDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff");        }
        #region
        private void SearchData()
        {
 
            //是否使用代理状态变量
            string strProxyStatus = ConfigurationManager.AppSettings["ProxyStatus"].ToString();
            if ("true".Equals(strProxyStatus))
            { 
                //代理字符串
                string strProxy = ConfigurationManager.AppSettings["ProxyServerAddress"].ToString();
                //添加代理
                AIV_UI.QuickWin.WinProxy.RefreshIESettings(strProxy);
            }            //打开初始页面            //cpTimer.Start();
            //againTimer.Start();
            
            //WbESL.Navigate("https://am-i.serviceportal.hp.com/pls/onbsm/tng_html.p_application?RememberMe=false&RememberMe=false&RememberMe=false&RememberMe=false");            bool isFirst = true;
            if (isFirst)
            {
                //页面加载完成监听事件
               this.wbESL.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbESL_DocumentCompleted);
               this.wbESL.ProgressChanged += new WebBrowserProgressChangedEventHandler(wbESL_ProgressChanged);
                isFirst = false;
                //打开登陆页面
                wbESL.Navigate("https://am-i.serviceportal.hp.com/pls/onbsm/tng_html.p_application?RememberMe=false&RememberMe=false&RememberMe=false&RememberMe=false");            }
        }
        private void wbESL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (wbESL.ReadyState != WebBrowserReadyState.Complete)
            {
                return;
            }
            //得到当前的网址
            string url = wbESL.Url.ToString();
            //登录页面网址
            string url_logon = "https://am-i.serviceportal.hp.com/pls/onbsm/tng_html.p_application?RememberMe=false&RememberMe=false&RememberMe=false";
            //如果是登录页面
            if (url.IndexOf(url_logon) != -1)
            {
                //从参数中取得用户名和密码
                string strUserName = Utility.GlobalUserEmail;// "[email protected]"; 
                EmployeeFacade _Execute_GetUserList = new EmployeeFacade();
                EmployeeData UserList = _Execute_GetUserList.GetEmployeeInfo(strUserId);
                DataRow dr = UserList.Tables[EmployeeData.TABLE_EMPLOYEE].Rows[0];
                string strPassWord = "";
                if (dr[EmployeeData.NT_PASSWORD].ToString() != String.Empty)
                {
                    strPassWord = dr[EmployeeData.NT_PASSWORD].ToString();
                }
                else
                {
                    MessageBox.Show(Message.MASS_WRONG_NTPASSWORD, Message.SYSTEM_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);                }
                if (strPassWord != String.Empty)
                {
                    strPassWord = Encrypt.MyEncrypt.IsUnEncrypt(strPassWord);
                }
                //取得页面上的用户名和密码标签
                HtmlElement htmlUserName = wbESL.Document.GetElementById("USER");
                HtmlElement htmlPassWord = wbESL.Document.GetElementById("PASSWORD");
                //如果参数和标签都不为空
                if (strUserName != null && strPassWord != null && htmlUserName != null && htmlPassWord != null)
                {
                    htmlUserName.SetAttribute("value", "");
                    htmlPassWord.SetAttribute("value", "");
                    htmlUserName.SetAttribute("value", strUserName);
                    htmlPassWord.SetAttribute("value", strPassWord);
                    //调用submit方法
                    wbESL.Navigate("javascript:trimAndSubmit();");
                    return;
                }
                if (url.Equals("https://am-i.serviceportal.hp.com/pls/onbsm/tng_html.p_application?RememberMe=false&RememberMe=false&RememberMe=false&RememberMe=false"))
                {
                    wbESL.Navigate("https://am-i.serviceportal.hp.com/pls/onbsm/onb_process_view.show_page?sa_arg=1110281");
                    return;
                }
            }
        }
        #endregion
        private void cpTimer_Tick(object sender, EventArgs e)
        {
            Console.WriteLine(beginTime);
            int lijiTime = RTime * 1000;
            if (beginTime > lijiTime)
            {
                beginTime = 0;
                wbESL.Navigate("https://am-i.serviceportal.hp.com/pls/onbsm/tng_html.p_application?RememberMe=false&RememberMe=false&RememberMe=false");
            }
        }
        private void againTimer_Tick(object sender, EventArgs e)
        {
            beginTime += 1000;
        }        private void wbESL_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            int cp = Convert.ToInt32(e.CurrentProgress);
            if (cp == -1)
            {
                beginTime = 0;
            }
            //Console.WriteLine("当前 进度" + cp);
        }
    }
}

解决方案 »

  1.   

    自己调试吧,肯定是null对象,提示会告诉你在哪一行,自己看吧
      

  2.   

    总是在wbESL报错 说未将对象引用设置到对象的实例
      

  3.   

    this.wbESL.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbESL_DocumentCompleted);
    这出错  不知道为什么 我调了很长时间了
      

  4.   

    wbESL是一个webbrowser,它是否存在?它是否导航到了对应的网页?网页中有没有你指定的html元素。建议调试下。
      

  5.   

    this.wbESL.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbESL_DocumentCompleted);
    这出错  不知道为什么 我调了很长时间了