/// <summary> /// This can be called to prepare a range of data prior to usage. /// </summary> /// <param name="index"></param> /// <param name="take"></param> public void PreloadRangeAsync(int index, int take) { if (index >= TotalItems()) { return; } //don't start preloading new data until the previous data has been preloaded. if (_donePreloadingDataEvent != null) { _donePreloadingDataEvent.WaitOne(); } // if we're at the end of the data set, don't pick a range that's out of bounds. var numberToTake = Math.Min(take, TotalItems() - index); var range = CachedData.ToList().GetRange(index, numberToTake); //if all values are already cached, return them var firstDefaultIndex = range.FindIndex(s => s == default(S)); if (firstDefaultIndex == -1) { return; } _donePreloadingDataEvent = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(AsyncDataLoadingHandler, new DataLoadRequest(index, numberToTake)); }
/// <summary> /// Get a subset of the items. /// </summary> /// <param name="index">the index from which to start</param> /// <param name="take">the number of items to pull out.</param> /// <returns></returns> public S[] GetRange(int index, int take) { //don't start preloading new data until the previous data has been preloaded. //TODO allow for preloading multiple ranges at the same time. if (_donePreloadingDataEvent != null) { _donePreloadingDataEvent.WaitOne(); } if (index > TotalItems()) { throw new ArgumentException(string.Format("Index of: {0} was greater than total items: {1}", index, TotalItems())); } if (take == 0) { return(new S[0]); } else if (take < 0) { throw new ArgumentException(string.Format("For index {0}, take had a value of {1}.", index, take)); } // if we're at the end of the data set, don't pick a range that's out of bounds. var numberToTake = Math.Min(take, TotalItems() - index); var range = CachedData.ToList().GetRange(index, numberToTake); //if all values are already cached, return them var firstDefaultIndex = range.FindIndex(s => s == default(S)); if (firstDefaultIndex == -1) { return(range.ToArray()); } LoadAsNeeded(range, index); return(range.ToArray()); }