public void WriterThread() { //while download is ongoing : do while buffer < 100 do remove from queu and put in buffer till its @ 100 end write buffer to db; reset buffer end bool DownloadIsOngoing = ThreadList.Exists(x => x.IsAlive == true); while (DownloadIsOngoing) //while there is at least one thread executing { while (DownloadIsOngoing && BufferList.Count() < MaxObjectBuffer) //while buffer is not max size AND download threads are open { Match match = new Match(); if (QueuRef.TryDequeue(out match)) { BufferList.Add(match); } else { //if we fail to retrieve an item it might signal that the download is finished so we have to evaluate downloadisongoing //in fact this is the only place we should re evaluate it, its kind of a hack but it ensures that we continue to write to //db while there are items left in the queu even though the download can be done already DownloadIsOngoing = ThreadList.Exists(x => x.IsAlive == true); } } //bufferlist now has 100 items in it, time to write Writer.InsertRange(BufferList); //raise event //technically not the highest id but should be close ReferenceToParent.OnDataWritten(new DownloaderEventArgs() { AmountWritten = BufferList.Count, HighestMatchIdWritten = BufferList.Last().match_id }); log.Info("Wrote some matches, count is " + BufferList.Count.ToString()); BufferList.Clear(); //and reset it } //raise event ReferenceToParent.OnDownloadIsFinished(); //cleanup the threads ThreadList.Clear(); }