Пример #1
0
        public Hunter(HunterConsole oh, HunterConfig config, ProjectInfo _pj, HunterForm main)
        {
            try
            {
                MainForm       = main;
                Error          = false;
                mHunterConsole = oh;
                projectInfo    = _pj;
                mHunterConfig  = config;
                ProxyFetcher   = new HunterProxyFetcher(AvailableProxies);
                projectInfo    = ProjectInfo.LoadProject(_pj.mHunterConsole, _pj.projectPath, _pj.strategyPath, true);

                downloadThreadNum = int.Parse(projectInfo.threadnum);
                hunterThreads     = new HunterDownloadThread[downloadThreadNum];

                //获取代理的线程
                ProxyGetThreads = new Thread[downloadThreadNum];

                if (mHunterConfig.UseProxy == true)
                {
                    FileStream   fs = new FileStream("proxy.hip", FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);
                    ProxyText = sr.ReadToEnd();
                    sr.Close();
                    fs.Close();
                    AllProxies = HunterProxy.GetProxy(ProxyText, mHunterConfig.ProxyFilterKeywords);
                }

                mHunterConsole.WriteMessage(projectInfo.ConfigInformation());
                mHunterConsole.WriteMessage("");
                mHunterConsole.WriteMessage(projectInfo.strategy.GetStrategyInformation());

                xmlDatabase = new XMLDatabase(projectInfo.database, mHunterConsole);
                xmlDatabase.openDatabase();

                try
                {
                    if (downloadThreadNum <= 0)
                    {
                        mHunterConsole.WriteMessage("配置错误:下载线程数不能小于0。");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    mHunterConsole.WriteException(ex);
                    return;
                }

                for (int i = 0; i < hunterThreads.Length; i++)
                {
                    hunterThreads[i] = new HunterDownloadThread();
                    hunterThreads[i].downloadThread = new Thread(threadDownloadUris);
                }

                if (mHunterConfig.UseProxy)
                {
                    for (int i = 0; i < ProxyGetThreads.Length; i++)
                    {
                        ProxyGetThreads[i] = new Thread(GetAvaliableProxies);
                    }
                }

                if (projectInfo.strategy.Keywords.Count <= 0)
                {
                    projectInfo.mHunterConsole.WriteMessage("没有找到关键字,任务取消。");
                    Error = true;
                    return;
                }

                thHuntUris = new Thread(threadHuntUris);
                thHuntUris.SetApartmentState(ApartmentState.STA);

                hUri = new HunterUri(this);
                projectInfo.strategy.RecordFirstWord();

                mHunterConsole.WriteMessage("下载线程总数:" + hunterThreads.Length);
                mHunterConsole.WriteMessage("读取配置完毕。");
                mHunterConsole.WriteMessage("正在运行任务...");
            }
            catch (Exception e)
            {
                mHunterConsole.WriteException(e);
            }
        }