本文整理汇总了C#中ConcurrentDictionary.ToList方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentDictionary.ToList方法的具体用法?C# ConcurrentDictionary.ToList怎么用?C# ConcurrentDictionary.ToList使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentDictionary
的用法示例。
在下文中一共展示了ConcurrentDictionary.ToList方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PostCreatedFiles
public static void PostCreatedFiles(ConcurrentDictionary<string, LocalFile> files, string uploadUrl)
{
var localFiles = files.ToList();
// Create post content
var content = new MultipartFormDataContent();
// Check we have content to send
if (!LoadFileToDataContent(content, files.Select(x => x.Value).ToList()))
{
Console.WriteLine("Failed to load file or no content as loaded. Cancel upload");
return;
}
SendPostData(content, uploadUrl);
if (BaseConfig.Synchronize)
{
localFiles.ForEach(x => x.Value.Synchronized = true);
}
else
{
// Remove files from queue
foreach (var file in localFiles)
{
LocalFile deleted;
if (!files.TryRemove(file.Key, out deleted))
{
Console.WriteLine("Failed to remove");
}
}
}
}
示例2: CalculateDistances
private static IEnumerable<KeyValuePair<string, double>> CalculateDistances(FansubFile file, IEnumerable<string> folders, Strategy.ComparisonStrategy strategy)
{
var bagOfResults = new ConcurrentDictionary<string, double>();
var justFolders = folders.ToDictionary(key => key, value => StringUtilities.GetJustFolderName(value));
Parallel.ForEach(justFolders, folder =>
{
var projectedStrategy = strategy.Invoke(file.SeriesName, folder.Value);
var seriesName = projectedStrategy.Item1;
var projectedFolderName = projectedStrategy.Item2;
bagOfResults.TryAdd(folder.Key, StringMetricsCalculator.Instance.MeasureSimilarity(seriesName, projectedFolderName));
});
var listResult = bagOfResults.ToList();
listResult.Sort(KeyValuePairComparison);
return listResult;
}
示例3: MeasureDistances
private IList<KeyValuePair<string, double>> MeasureDistances(FansubFile file, Func<string, string, double> metricFunc, IEnumerable<string> knownFolders)
{
var bagOfResults = new ConcurrentDictionary<string, double>();
var seriesName = file.SeriesName;
Parallel.ForEach(knownFolders, folder =>
{
var folderName = GetJustFolderName(folder);
bagOfResults.TryAdd(folder, metricFunc.Invoke(seriesName, folderName));
});
var listResult = bagOfResults.ToList();
listResult.Sort((a, b) =>
{
//We want to sort from largest to smallest, so we reverse the comparison
var result = b.Value - a.Value;
if (result < 0) return -1;
if (result > 0) return 1;
return 0;
});
return listResult;
}
示例4: GetVoteDictionary
public IOrderedEnumerable<KeyValuePair<DateTime, List<string>>> GetVoteDictionary()
{
ConcurrentDictionary<DateTime, List<string>> d = new ConcurrentDictionary<DateTime, List<string>>();
foreach (var vote in Votes)
{
d.AddOrUpdate(vote.Time, new List<string> { vote.UserName }, (k, v) => AddToList(v, vote.UserName));
}
var kvPairs = d.ToList().OrderByDescending(x => x.Value.Count);
return kvPairs;
}
示例5: RunDictionaryTest_Add1
private static bool RunDictionaryTest_Add1(int cLevel, int initSize, int threads, int addsPerThread)
{
TestHarness.TestLog(
"* RunDictionaryTest_Add1(cLevel={0}, initSize={1}, threads={2}, addsPerThread={3})",
cLevel, initSize, threads, addsPerThread);
IDictionary<int, int> dict = new ConcurrentDictionary<int, int>(cLevel, 1);
int count = threads;
using (ManualResetEvent mre = new ManualResetEvent(false))
{
for (int i = 0; i < threads; i++)
{
int ii = i;
ThreadPool.QueueUserWorkItem(
(o) =>
{
for (int j = 0; j < addsPerThread; j++)
{
dict.Add(j + ii * addsPerThread, -(j + ii * addsPerThread));
}
if (Interlocked.Decrement(ref count) == 0) mre.Set();
});
}
mre.WaitOne();
}
if (dict.Any(pair => pair.Key != -pair.Value))
{
TestHarness.TestLog(" > Invalid value for some key in the dictionary.");
return false;
}
var gotKeys = dict.Select(pair => pair.Key).OrderBy(i => i).ToArray();
var expectKeys = Enumerable.Range(0, threads * addsPerThread);
if (!gotKeys.SequenceEqual(expectKeys))
{
TestHarness.TestLog(" > The set of keys in the dictionary is invalid.");
return false;
}
// Finally, let's verify that the count is reported correctly.
int expectedCount = threads * addsPerThread;
if (dict.Count != expectedCount || dict.ToArray().Length != expectedCount || dict.ToList().Count() != expectedCount)
{
TestHarness.TestLog(" > Incorrect count of elements reported for the dictionary.");
return false;
}
return true;
}
示例6: RunDictionaryTest
private static bool RunDictionaryTest(int cLevel, int initSize, int threads, int addsPerThread, TestMethod testMethod)
{
TestHarness.TestLog("* RunDictionaryTest_{0}, Level={1}, initSize={2}, threads={3}, addsPerThread={4})",
PrintTestMethod(testMethod), cLevel, initSize, threads, addsPerThread);
ConcurrentDictionary<int, int> dict = new ConcurrentDictionary<int, int>(cLevel, 1);
int count = threads;
using (ManualResetEvent mre = new ManualResetEvent(false))
{
for (int i = 0; i < threads; i++)
{
int ii = i;
ThreadPool.QueueUserWorkItem(
(o) =>
{
for (int j = 0; j < addsPerThread; j++)
{
//call either of the two overloads of GetOrAdd
if (j + ii % 2 == 0)
{
dict.GetOrAdd(j, -j);
}
else
{
dict.GetOrAdd(j, x => -x);
}
}
if (Interlocked.Decrement(ref count) == 0) mre.Set();
});
}
mre.WaitOne();
}
bool passed = true;
if (dict.Any(pair => pair.Key != -pair.Value))
{
TestHarness.TestLog(" > Invalid value for some key in the dictionary.");
passed = false;
}
var gotKeys = dict.Select(pair => pair.Key).OrderBy(i => i).ToArray();
var expectKeys = Enumerable.Range(0, addsPerThread);
if (!gotKeys.SequenceEqual(expectKeys))
{
TestHarness.TestLog(" > The set of keys in the dictionary is invalid.");
passed = false;
}
// Finally, let's verify that the count is reported correctly.
int expectedCount = addsPerThread;
int count1 = dict.Count, count2 = dict.ToArray().Length,
count3 = dict.ToList().Count;
if (count1 != expectedCount || count2 != expectedCount || count3 != expectedCount)
{
TestHarness.TestLog(" > Incorrect count of elements reported for the dictionary. Expected {0}, Dict.Count {1}, ToArray.Length {2}, ToList.Count {3}",
expectedCount, count1, count2, count3);
passed = false;
}
return passed;
}
示例7: RunDictionaryTest_Remove3
private static bool RunDictionaryTest_Remove3()
{
TestHarness.TestLog("* RunDictionaryTest_Remove3()");
ConcurrentDictionary<int, int> dict = new ConcurrentDictionary<int, int>();
dict[99] = -99;
ICollection<KeyValuePair<int, int>> col = dict;
// Make sure we cannot "remove" a key/value pair which is not in the dictionary
for (int i = 0; i < 1000; i++)
{
if (i != 99)
{
if (col.Remove(new KeyValuePair<int, int>(i, -99)) || col.Remove(new KeyValuePair<int, int>(99, -i)))
{
TestHarness.TestLog(" > Removed a key/value pair which was not supposed to be in the dictionary.");
return false;
}
}
}
// Can we remove a key/value pair successfully?
if (!col.Remove(new KeyValuePair<int, int>(99, -99)))
{
TestHarness.TestLog(" > Failed to remove a key/value pair which was supposed to be in the dictionary.");
return false;
}
// Make sure the key/value pair is gone
if (col.Remove(new KeyValuePair<int, int>(99, -99)))
{
TestHarness.TestLog(" > Removed a key/value pair which was not supposed to be in the dictionary.");
return false;
}
// And that the dictionary is empty. We will check the count in a few different ways:
if (dict.Count != 0 || dict.ToArray().Count() != 0 || dict.ToList().Count() != 0)
{
TestHarness.TestLog(" > Incorrect count of elements reported for the dictionary.");
return false;
}
return true;
}
示例8: RunDictionaryTest_Remove1
private static bool RunDictionaryTest_Remove1(int cLevel, int threads, int removesPerThread)
{
TestHarness.TestLog("* RunDictionaryTest_Remove1(cLevel={0}, threads={1}, removesPerThread={2})", cLevel, threads, removesPerThread);
ConcurrentDictionary<int, int> dict = new ConcurrentDictionary<int, int>(cLevel, 1);
int N = 2 * threads * removesPerThread;
for (int i = 0; i < N; i++) dict[i] = -i;
// The dictionary contains keys [0..N), each key mapped to a value equal to the key.
// Threads will cooperatively remove all even keys.
int running = threads;
bool passed = true;
using (ManualResetEvent mre = new ManualResetEvent(false))
{
for (int i = 0; i < threads; i++)
{
int ii = i;
ThreadPool.QueueUserWorkItem(
(o) =>
{
for (int j = 0; j < removesPerThread; j++)
{
int value;
int key = 2 * (ii + j * threads);
if (!dict.TryRemove(key, out value))
{
TestHarness.TestLog(" > Failed to remove an element, which should be in the dictionary.");
passed = false;
break;
}
if (value != -key)
{
TestHarness.TestLog(" > Invalid value for some key in the dictionary.");
passed = false;
break;
}
}
if (Interlocked.Decrement(ref running) == 0) mre.Set();
});
}
mre.WaitOne();
}
if (!passed) return false;
var res = dict.ToArray();
if (res.Any(pair => pair.Key != -pair.Value))
{
TestHarness.TestLog(" > Invalid value for some key in the dictionary.");
return false;
}
IEnumerable<int> gotKeys = res.Select(pair => pair.Key).OrderBy(i => i);
IEnumerable<int> expectKeys = Enumerable.Range(0, threads * removesPerThread).Select(i => 2 * i + 1);
if (!gotKeys.SequenceEqual(expectKeys))
{
TestHarness.TestLog(" > The set of keys in the dictionary is invalid.");
return false;
}
// Finally, let's verify that the count is reported correctly.
int expectedCount = expectKeys.Count();
if (dict.Count != expectedCount || dict.ToArray().Length != expectedCount || dict.ToList().Count() != expectedCount)
{
TestHarness.TestLog(" > Incorrect count of elements reported for the dictionary.");
return false;
}
return true;
}