private void ChunkLoadCallback(object callbackData) { // Cast the callback object to the data object, serialize and load the chunk using this data // and then add it to the loaded chunk queue ChunkLoadData chunk = (ChunkLoadData)callbackData; loadedChunkQueue.Enqueue(fileLoader.DeserializeAndLoadFileChunk(currentFileName, chunk.ChunkByteIndex, chunk.ChunkSize)); // Finally, invoke the chunk finished event ChunkFinishedLoad.Invoke(this, new EventArgs()); }
// Handler that is triggered when a chunk has been loaded private void LoadChunkFinishedHandler(object sender, EventArgs e) { // If there are more chunks in the load queue, dequeue the next chunk // to load object and add its callback with data to the thread pool for // execution if (chunksToLoadQueue.Count != 0) { ChunkLoadData chunk = chunksToLoadQueue.Dequeue(); ThreadPool.QueueUserWorkItem(ChunkLoadCallback, chunk); } else { // Otherwise, the manager has finished loading data, so flip the // relevant flags IsLoading = false; } }
public KinectFramesStore GetNextChunk() { // First check to see if any chunks are available in the queue, returning // a null reference if there aren't if (loadedChunkQueue.Count == 0) { return(null); } // Otherwise, dequeue the next chunk KinectFramesStore nextChunk = loadedChunkQueue.Dequeue(); // If the file chunk index is not at the last position, there is more data // left in the file if (fileChunkIndex < FileInfoOpen.ChunkSizes.Count - 1) { // If the loaded chunk queue is now equla to or under the buffer size, // stop playback and load in more chunks to refresh the buffer if (loadedChunkQueue.Count <= ChunkBufferSize) { StopPlaying(); // Check that the number of chunks left in the file is greater than the number // of chunks to load in to refill the buffer. If it is not, use the remaining // number of chunks in the file instead int lastChunkIndex = fileChunkIndex; if (RefreshBufferSize > (FileInfoOpen.ChunkSizes.Count - 1 - fileChunkIndex)) { fileChunkIndex = FileInfoOpen.ChunkSizes.Count - 1; } else { fileChunkIndex += RefreshBufferSize; } // If a chunk is still loading as well, simply queue up all of these new chunks if (IsLoading) { for (int i = lastChunkIndex + 1; i < fileChunkIndex + 1; i++) { chunksToLoadQueue.Enqueue(new ChunkLoadData(GetChunkStartFromIndex(i), FileInfoOpen.ChunkSizes[i])); } } else { // Otherwise, if there is more than one chunk to load, skip over the first chunk // in this new set and queue up all of the others, including the chunk that the // global index is currently on if (fileChunkIndex - lastChunkIndex > 1) { for (int i = lastChunkIndex + 2; i < fileChunkIndex + 1; i++) { chunksToLoadQueue.Enqueue(new ChunkLoadData(GetChunkStartFromIndex(i), FileInfoOpen.ChunkSizes[i])); } } // Then, begin loading in the first of these new chunks ThreadPool.QueueUserWorkItem(ChunkLoadCallback, new ChunkLoadData(GetChunkStartFromIndex(lastChunkIndex + 1), FileInfoOpen.ChunkSizes[lastChunkIndex + 1])); } } else { // Otherwise, progress the file chunk index and load in the next chunk to replace // the one just dequeued fileChunkIndex++; ChunkLoadData newChunkToLoad = new ChunkLoadData(GetChunkStartFromIndex(fileChunkIndex), FileInfoOpen.ChunkSizes[fileChunkIndex]); // If another chunk is already loading, add this new chunk into the chunks to load // queue. Otherwise, manually queue the chunk processing task on the background thread if (IsLoading) { chunksToLoadQueue.Enqueue(newChunkToLoad); } else { ThreadPool.QueueUserWorkItem(ChunkLoadCallback, newChunkToLoad); } } } else if (IsLoading) { // Otherwise, if it is at the last position and loading data in, stop playing so that the // final chunks can be added to the buffer before resuming playing StopPlaying(); } return(nextChunk); }