public static bool Dammi(FrameworkElement ogg, KeyValueTuple <string, object> tag, ref object valore, bool errSeMancante = true) { if (CtrlParametri(ogg, tag) == false) { return(false); } if (ogg.Tag == null) { if (errSeMancante == true) { Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Nel controllo ogg.Name:<" + ogg.Name + "> manca la tag di nome:<" + tag.Key + ">, ogg.Tag è a nothing ")); } return(false); } bool esiste; if (Concur.Dictionary_TryGet((ConcurrentDictionary <string, object>)ogg.Tag, tag.Key, ref valore, out esiste) == false) { if (esiste == true) { Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Non sono riuscito a leggere la tag di nome:<" + tag.Key + "> sul controllo ogg.Name:<" + ogg.Name + ">")); } else { if (errSeMancante == true) { Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Nel controllo ogg.Name:<" + ogg.Name + "> manca la tag di nome:<" + tag.Key + ">")); } } return(false); } return(true); }
public static void SbloccaThrPadre() { var idThrFiglio = Thread.CurrentThread.ManagedThreadId; if (listaAttesaThr.ContainsKey(idThrFiglio) == false) { Log.main.Add(new Mess(LogType.Warn, Log.main.warnUserText, "Nella listaAttesaThr non c'è il seguente thrId:<" + idThrFiglio + ">")); return; } listaAttesaThr[idThrFiglio].Set(); //Sblocca il ThrPadre Concur.Dictionary_TryRemove(listaAttesaThr, idThrFiglio); }
public static bool Elimina(FrameworkElement ogg, KeyValueTuple <string, object> tag) { if (CtrlParametri(ogg, tag) == false) { return(false); } if (Concur.Dictionary_TryRemove((ConcurrentDictionary <string, object>)ogg.Tag, tag) == false) { Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Non sono riuscito a rimuovere la tag di nome:<" + tag.Key + "> sul controllo ogg.Name:<" + ogg.Name + ">")); return(false); } return(true); }
public static Thread AvviaNuovo(ThreadStart start, ApartmentState apartment = ApartmentState.STA) { AutoResetEvent segnaleAttesa = new AutoResetEvent(false); Thread thr = new Thread(start); thr.SetApartmentState(apartment); thr.IsBackground = true; Concur.Dictionary_TryAddOrUpdate(listaAttesaThr, new KeyValuePair <Int32, AutoResetEvent>(thr.ManagedThreadId, segnaleAttesa), noUpadate: true); thr.Start(); if (segnaleAttesa.WaitOne(1000) == false) { //Significa che è andato in timeout e quindi il thread chiamato non ha eseguito la set sull'AutoResetEvent contenuto nella listaAttesaThr Log.main.Add(new Mess(LogType.Warn, Log.main.warnUserText, "Il thr figlio non ha eseguito la .set() sull'AutoResetEvent contenuto nella listaAttesaThr")); } return(thr); }
public void Add(UploadItem elemUpload) { if (elemUpload == null) { Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "Ricevuto elemUpload a nothing")); return; } if (elemUpload.TimeoutSec == 0) { elemUpload.TimeoutSec = Convert.ToInt16(this.config.DefaultTimeoutSec); } idItem += 1; elemUpload.IdItemInList = idItem; Concur.Dictionary_TryAddOrUpdate(queue, new KeyValuePair <UInt64, UploadItem>(idItem, elemUpload), noUpadate: true); }
private void ControlloElementiEStatistiche() { Int32 elemDaElim; UInt64 totalizzatoreKBSec; bool raggiuntoMaxKBSecTmp, elementoDiInteresse; IOrderedEnumerable <KeyValuePair <UInt64, UploadItem> > chiaviValori; raggiuntoMaxKBSecTmp = false; totalizzatoreKBSec = 0; chiaviValori = from tmp in this.queue where tmp.Value.UploadState == UplItemState.UplCompletato || tmp.Value.UploadState == UplItemState.TimeoutToStart || tmp.Value.UploadState == UplItemState.Timeout || tmp.Value.UploadState == UplItemState.Eccezione orderby tmp.Key ascending select tmp; elemDaElim = chiaviValori.Count() - this.config.MaxItemInStatisticsQueue; if (elemDaElim < 0) { elemDaElim = 0; } //For Each chiaveValore In From c In Me.codaStatistiche 'Può dare errori se gli si modifica il numero degli elementi del dictionary foreach (KeyValuePair <UInt64, UploadItem> chiaveValore in chiaviValori) { elementoDiInteresse = false; CheckUploadSubsetEnded(chiaveValore, ref elementoDiInteresse); if (raggiuntoMaxKBSecTmp == false && this.config.MaxKBSec > 0) { ControlloKBSec(chiaveValore, ref totalizzatoreKBSec, ref raggiuntoMaxKBSecTmp, ref elementoDiInteresse); } if (elemDaElim > 0 && elementoDiInteresse == false) { Concur.Dictionary_TryRemove(this.queue, chiaveValore.Key); elemDaElim -= 1; continue; } } this.raggiuntoMaxKBSec = raggiuntoMaxKBSecTmp; }
private void ThrAvvioDownLoad(UInt64 itemKey) { Thr.SbloccaThrPadre(); Thread thrDL; thrDL = Thr.AvviaNuovo(() => ThrDownload(itemKey)); DownloadItem elemDownload = null; if (Concur.Dictionary_TryGet(this.queue, itemKey, ref elemDownload, out _) == false) { return; } if (Thr.AttesaCompletamento(ref thrDL, elemDownload.TimeoutSec == -1 ? -1 : elemDownload.TimeoutSec * 1000) == false) { //TimeOut elemDownload.DescErr = "Timeout:<" + elemDownload.TimeoutSec + "> secondi, raggiunto"; elemDownload.DownloadState = DwlItemState.Timeout; Log.main.Add(new Mess(elemDownload.tipoLogTimeout, "", "per l'url:<" + elemDownload.url + ">, " + elemDownload.DescErr)); } }
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 }
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(); } }