Exemple #1
0
        public void f_runLoop(object state, bool timedOut)
        {
            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                ti.f_stopJob();
                return;
            }

            lock (_lock)
                if (isDownloading)
                {
                    return;
                }

            string _url = StoreJob.f_url_getUrlPending();

            if (_url.Length == 0)
            {
                return;
            }

            //Interlocked.CompareExchange(ref Id, Id, 0);
            Tracer.WriteLine("J{0} -> {1}", Id, _url);

            HttpWebRequest w = (HttpWebRequest)WebRequest.Create(new Uri(_url));

            w.BeginGetResponse(asyncResult =>
            {
                string url     = ((HttpWebRequest)asyncResult.AsyncState).RequestUri.ToString();
                string data    = string.Empty;
                bool isSuccess = true;
                try
                {
                    HttpWebResponse rs = (HttpWebResponse)w.EndGetResponse(asyncResult);
                    if (rs.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader sr = new StreamReader(rs.GetResponseStream(), System.Text.Encoding.UTF8))
                            data = sr.ReadToEnd();
                        rs.Close();
                    }

                    if (!string.IsNullOrEmpty(data))
                    {
                        data = HttpUtility.HtmlDecode(data);
                        data = format_HTML(data);

                        string[] urls = get_UrlHtml(url, data);
                        if (urls.Length > 0)
                        {
                            StoreJob.f_url_AddRange(urls);
                        }
                    }
                    else
                    {
                        isSuccess = false;
                        data      = "REQUEST_FAIL";
                    }
                }
                catch (Exception ex)
                {
                    data      = ex.Message;
                    isSuccess = false;
                }

                Tracer.WriteLine("J{0} <- {1}", Id, _url);

                StoreJob.f_url_countResult(_url, data, isSuccess);

                int urlCounter = StoreJob.f_url_countPending();
                if (urlCounter == 0)
                {
                    bool end = StoreJob.f_url_stateJobIsComplete(Id);
                    if (end)
                    {
                        StoreJob.f_url_Complete();
                    }
                    return;
                }

                lock (_lock)
                    isDownloading = false;
            }, w);
        }