本文整理汇总了C#中ConcurrentStack.ToDictionary方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.ToDictionary方法的具体用法?C# ConcurrentStack.ToDictionary怎么用?C# ConcurrentStack.ToDictionary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.ToDictionary方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartDownloading
// needed for Unit Testing
internal static void StartDownloading(CancellationToken ct, FailureToken ft,
ILargeFileDownloadParameters parameters,
IAsyncProgress<LargeFileDownloadProgressChangedEventArgs> progress = null,
Action<string> logger = null,
BufferManager bufferManager = null)
{
//create the file
Stream stream = parameters.GetOutputStream();
if (parameters.FileSize == 0) // Terminate Zero size files
{
if (progress != null)
{
progress.Report(new LargeFileDownloadProgressChangedEventArgs(100, 0, 0,
parameters.FileSize,
parameters.FileSize, "",
"",
null));
}
if (parameters.AutoCloseStream)
stream.Close();
return;
}
//figure out number of chunks
int chunkCount = GetChunkCount(parameters.FileSize, parameters.MaxChunkSize);
int numberOfThreads = Math.Min(parameters.MaxThreads, chunkCount);
logger = logger ?? ((s) => { });
var downloadWorkers = new List<Downloader>(numberOfThreads);
var chunksWritten = new Dictionary<int, bool>();
bool isFailed = false;
long totalBytesWritten = 0;
double byteWriteRate = 0.0;
try
{
var readStack = new ConcurrentStack<int>();
//add all of the chunks to the stack
var rangeArray = Enumerable.Range(0, chunkCount).Reverse().ToArray();
readStack.PushRange(rangeArray);
chunksWritten = readStack.ToDictionary(k => k, v => false);
var writeQueue = new ConcurrentQueue<ChunkedFilePart>();
// ReSharper disable AccessToModifiedClosure
Func<int, bool> downloadThrottle = (int c) => writeQueue.Count > 30;
// ReSharper restore AccessToModifiedClosure
if (bufferManager == null)
{
bufferManager = new BufferManager(new[]
{
new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint) numberOfThreads),
new BufferQueueSetting((uint) parameters.MaxChunkSize, (uint) numberOfThreads)
});
}
int expectedChunkDownloadTime = ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize);
for (int i = 0; i < numberOfThreads; i++)
{
downloadWorkers.Add(new Downloader(bufferManager, parameters, writeQueue, readStack,
downloadThrottle, expectedChunkDownloadTime, ft, logger, ct));
}
//start all the download threads
downloadWorkers.ForEach(x => x.Start());
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
long oldElapsedMilliSeconds = watch.ElapsedMilliseconds;
DateTime lastWriteTime = DateTime.MaxValue;
long lastPointInFile = 0;
int kc = 0;
//start the write loop
while (chunksWritten.Any(kvp => !kvp.Value) && !ct.IsCancellationRequested && !ft.FailureDetected)
{
ChunkedFilePart part;
while (writeQueue.TryDequeue(out part) && !ft.FailureDetected)
{
//retry?
logger(string.Format("[{1}] writing chunk: {0}", part.Chunk, parameters.Id));
stream.Position = part.FileOffset;
stream.Write(part.Content, 0, part.Length);
totalBytesWritten += part.Length;
bufferManager.FreeBuffer(part.Content);
chunksWritten[part.Chunk] = true;
lastWriteTime = DateTime.Now;
if (progress != null)
{
var elapsed = watch.ElapsedMilliseconds;
var diff = elapsed - oldElapsedMilliSeconds;
if (diff > 2000)
{
long bytesDownloaded = (long)chunksWritten.Count(kvp => kvp.Value) * parameters.MaxChunkSize;
long interimReads = bytesDownloaded + part.Length - lastPointInFile;
byteWriteRate = (interimReads / (diff / (double)1000));
lastPointInFile += interimReads;
//.........这里部分代码省略.........