Exemplo n.º 1
0
        protected override void ThrCicloReadInputBufferAndElaborateMainQueue()
        {
            Thr.SbloccaThrPadre();
            DateTime timeoutStart;
            //UInt64 indiceProssElem;
            IOrderedEnumerable <KeyValuePair <UInt64, DownloadItem> > elementiDaDwl;
            DownloadItem elemDownload = null;

            //indiceProssElem = 1;

#if DEBUG == false
            try {
#endif
            while (true)
            {
                Thread.Sleep(9);

                while (inputBuffer.Count > 0)
                {
                    if (Concur.Queue_TryDequeue(inputBuffer, ref elemDownload) == false)
                    {
                        break;
                    }
                    idItem += 1;
                    elemDownload.IdItemInList = idItem;
                    Concur.Dictionary_TryAddOrUpdate(queue, new KeyValuePair <UInt64, DownloadItem>(idItem, elemDownload), noUpadate: true);
                }

                ControlloElementiEStatistiche();

                ItemInProcessing = (from tmp in this.queue where tmp.Value.DownloadState == DwlItemState.DwlAvviato select tmp).Count();

                if (ItemInProcessing >= this.config.MaxParallelStreams)
                {
                    continue;
                }

                if (this.raggiuntoMaxKBSec)
                {
                    continue;
                }

                elementiDaDwl = from tmp in this.queue where tmp.Value.DownloadState == DwlItemState.Iniziale orderby tmp.Value.Priority ascending, tmp.Key ascending select tmp; //A parità di priority prendo il più vecchio in coda

                ItemToProcess = elementiDaDwl.Count();

                if (ItemToProcess == 0)
                {
                    continue;
                }

                elemDownload = elementiDaDwl.ElementAt(0).Value; //Serve poichè gli elementi della LinQ si aggiornano all'istante quando un parametro della where o della orderby cambiano

                Thread thrDL = Thr.AvviaNuovo(() => ThrAvvioDownLoad(elementiDaDwl.ElementAt(0).Key));

                timeoutStart = DateTime.Now;

                while (true) //Check operation start on item
                {
                    if (elemDownload.DownloadState != DwlItemState.Iniziale)
                    {
                        break;                                                      //operation started
                    }
                    if ((DateTime.Now - timeoutStart).TotalSeconds > timeoutToStartSec)
                    {
                        elemDownload.DescErr = "TimeoutToStart di " + timeoutToStartSec + " secondi raggiunto, url:<" + elemDownload.url + ">";
                        Log.main.Add(new Mess(elemDownload.tipoLogTimeout, "", "per l'url:<" + elemDownload.url + ">, " + elemDownload.DescErr));
                        elemDownload.DownloadState = DwlItemState.TimeoutToStart;
                        break;
                    }
                    Thread.Sleep(1);
                }

                Thread.Sleep(config.SleepAfterOneOperationMs);

                //while (elemDownload.DownloadState == DwlItemState.DwlAvviato)
                //{
                //    Thread.Sleep(1);
                //}
                //indiceProssElem = elementiDaDwl.ElementAt(0).Key + 1;
            }
#if DEBUG == false
        }

        catch (Exception ex) {
            Thr.NotificaErrThrCiclo(ex, true);
        }
#endif
        }
Exemplo n.º 2
0
        private void ThrDownload(UInt64 itemKey)
        {
            Thr.SbloccaThrPadre();
            DateTime  tmpAttesa;
            WebClient client = new WebClient();

            //client = new WebClient(); //TODO decommentare //, flusso As New IO.MemoryStream
            DownloadItem elemDownload = null;

            try
            {
                if (Concur.Dictionary_TryGet(this.queue, itemKey, ref elemDownload, out _) == false)
                {
                    return;
                }

                client.DownloadDataCompleted   += DownloadTerminato;
                client.DownloadProgressChanged += PercentualeDownload;

                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); //Aggiungo lo user agent nell'header nel caso la richiesta contiene una query.
                elemDownload.OperationStartDate = DateTime.Now;
                elemDownload.DownloadState      = DwlItemState.DwlAvviato;

                client.DownloadDataAsync(new Uri(elemDownload.url), elemDownload);
                //client.DownloadData() //Non scatena gli eventi DownloadProgressChanged e DownloadDataCompleted

                //AttesaBoolean(client.IsBusy, -1) //Per le proprità(come IsBusy) se cambia di valore sembra che non lo sente quindi non posso utilizzare AttesaBoolean
                while (client.IsBusy)
                {
                    Thread.Sleep(1);
                }

                tmpAttesa = DateTime.Now;

                while (elemDownload.DownloadState == DwlItemState.DwlAvviato)
                {
                    if (DataOra.AttesaTempo(ref tmpAttesa, 1000) == true)
                    {
                        Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Da quando client non è Busy, lo statoDownload non si è aggiornato in tempo utile, elemDownload.url:<" + elemDownload.url + ">", visualMsgBox: false));
                        elemDownload.DownloadState = DwlItemState.Eccezione;
                        break;
                    }
                }

                //client.Dispose();
            }
            catch (ThreadAbortException ex)  //Thread interrupted by abort, do nothing
            { var tmp = ex; }
            catch (Exception ex)
            {
                Log.main.Add(new Mess(elemDownload.tipoLogEccezione, "", "Eccezione per l'url:<" + elemDownload.url + ">, ex.mess:<" + ex.Message + ">"));
                elemDownload.DescErr       = ex.Message;
                elemDownload.DownloadState = DwlItemState.Eccezione;
            }
            finally
            {
                //client.CancelAsync(); //TODO verificare se è la causa dell'errore  sul sito investing

                client.CancelAsync();
                client.Dispose();
            }
        }