本文整理汇总了C#中ConcurrentSet.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentSet.Add方法的具体用法?C# ConcurrentSet.Add怎么用?C# ConcurrentSet.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentSet
的用法示例。
在下文中一共展示了ConcurrentSet.Add方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: should_add_an_item
public void should_add_an_item()
{
var setToRemoveFrom = new ConcurrentSet<int>(Enumerable.Range(1, 5));
setToRemoveFrom.Add(6);
setToRemoveFrom.ShouldEqual(new[] { 1, 2, 3, 4, 5, 6 });
}
示例2: ConcurrentTest
public void ConcurrentTest()
{
var cset = new ConcurrentSet<int>();
Action a1 = () => { for (int i = 0; i < 1000000; i++) cset.Add(i); };
Action a2 = () => { for (int i = 1000000; i < 2000000; i++) cset.Add(i); };
Action a3 = () => { for (int i = 2000000; i < 3000000; i++) cset.Add(i); };
Action a4 = () => { for (int i = 3000000; i < 4000000; i++) cset.Add(i); };
bool b1 = false;
bool b2 = false;
bool b3 = false;
bool b4 = false;
a1.BeginInvoke(iar =>
{
a1.EndInvoke(iar);
b1 = true;
}, null);
a2.BeginInvoke(iar =>
{
a2.EndInvoke(iar);
b2 = true;
}, null);
a3.BeginInvoke(iar =>
{
a3.EndInvoke(iar);
b3 = true;
}, null);
a4.BeginInvoke(iar =>
{
a4.EndInvoke(iar);
b4 = true;
}, null);
while (!(b1 && b2 && b3 && b4))
{
Thread.Sleep(10);
}
Assert.AreEqual(4000000, cset.Count());
}
示例3: DetermineAllSymbolsCoreAsync
private async Task DetermineAllSymbolsCoreAsync(ISymbol symbol, ConcurrentSet<ISymbol> result)
{
this.cancellationToken.ThrowIfCancellationRequested();
var searchSymbol = MapToAppropriateSymbol(symbol);
// 2) Try to map this back to source symbol if this was a metadata symbol.
searchSymbol = await SymbolFinder.FindSourceDefinitionAsync(searchSymbol, solution, cancellationToken).ConfigureAwait(false) ?? searchSymbol;
if (searchSymbol != null && result.Add(searchSymbol))
{
this.progress.OnDefinitionFound(searchSymbol);
this.foundReferences.GetOrAdd(searchSymbol, createSymbolLocations);
// get project to search
var projects = GetProjectScope();
this.cancellationToken.ThrowIfCancellationRequested();
List<Task> finderTasks = new List<Task>();
foreach (var f in finders)
{
finderTasks.Add(Task.Run(async () =>
{
var symbols = await f.DetermineCascadedSymbolsAsync(searchSymbol, solution, projects, cancellationToken).ConfigureAwait(false) ?? SpecializedCollections.EmptyEnumerable<ISymbol>();
this.cancellationToken.ThrowIfCancellationRequested();
List<Task> symbolTasks = new List<Task>();
foreach (var child in symbols)
{
symbolTasks.Add(Task.Run(() => DetermineAllSymbolsCoreAsync(child, result), this.cancellationToken));
}
await Task.WhenAll(symbolTasks).ConfigureAwait(false);
}, this.cancellationToken));
}
await Task.WhenAll(finderTasks).ConfigureAwait(false);
}
}
示例4: RunCore
private int RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, CancellationToken cancellationToken)
{
Debug.Assert(!Arguments.IsScriptRunner);
cancellationToken.ThrowIfCancellationRequested();
if (Arguments.DisplayLogo)
{
PrintLogo(consoleOutput);
}
if (Arguments.DisplayHelp)
{
PrintHelp(consoleOutput);
return Succeeded;
}
if (ReportErrors(Arguments.Errors, consoleOutput, errorLogger))
{
return Failed;
}
var touchedFilesLogger = (Arguments.TouchedFilesPath != null) ? new TouchedFileLogger() : null;
Compilation compilation = CreateCompilation(consoleOutput, touchedFilesLogger, errorLogger);
if (compilation == null)
{
return Failed;
}
var diagnostics = new List<DiagnosticInfo>();
var analyzers = ResolveAnalyzersFromArguments(diagnostics, MessageProvider, touchedFilesLogger);
var additionalTextFiles = ResolveAdditionalFilesFromArguments(diagnostics, MessageProvider, touchedFilesLogger);
if (ReportErrors(diagnostics, consoleOutput, errorLogger))
{
return Failed;
}
cancellationToken.ThrowIfCancellationRequested();
CancellationTokenSource analyzerCts = null;
AnalyzerManager analyzerManager = null;
AnalyzerDriver analyzerDriver = null;
try
{
Func<ImmutableArray<Diagnostic>> getAnalyzerDiagnostics = null;
ConcurrentSet<Diagnostic> analyzerExceptionDiagnostics = null;
if (!analyzers.IsDefaultOrEmpty)
{
analyzerCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
analyzerManager = new AnalyzerManager();
analyzerExceptionDiagnostics = new ConcurrentSet<Diagnostic>();
Action<Diagnostic> addExceptionDiagnostic = diagnostic => analyzerExceptionDiagnostics.Add(diagnostic);
var analyzerOptions = new AnalyzerOptions(ImmutableArray<AdditionalText>.CastUp(additionalTextFiles));
analyzerDriver = AnalyzerDriver.CreateAndAttachToCompilation(compilation, analyzers, analyzerOptions, analyzerManager, addExceptionDiagnostic, Arguments.ReportAnalyzer, out compilation, analyzerCts.Token);
getAnalyzerDiagnostics = () => analyzerDriver.GetDiagnosticsAsync(compilation).Result;
}
// Print the diagnostics produced during the parsing stage and exit if there were any errors.
if (ReportErrors(compilation.GetParseDiagnostics(), consoleOutput, errorLogger))
{
return Failed;
}
if (ReportErrors(compilation.GetDeclarationDiagnostics(), consoleOutput, errorLogger))
{
return Failed;
}
EmitResult emitResult;
// NOTE: as native compiler does, we generate the documentation file
// NOTE: 'in place', replacing the contents of the file if it exists
string finalPeFilePath;
string finalPdbFilePath;
string finalXmlFilePath;
Stream xmlStreamOpt = null;
cancellationToken.ThrowIfCancellationRequested();
finalXmlFilePath = Arguments.DocumentationPath;
if (finalXmlFilePath != null)
{
xmlStreamOpt = OpenFile(finalXmlFilePath, consoleOutput, PortableShim.FileMode.OpenOrCreate, PortableShim.FileAccess.Write, PortableShim.FileShare.ReadWriteBitwiseOrDelete);
if (xmlStreamOpt == null)
{
return Failed;
}
xmlStreamOpt.SetLength(0);
}
cancellationToken.ThrowIfCancellationRequested();
IEnumerable<DiagnosticInfo> errors;
using (var win32ResourceStreamOpt = GetWin32Resources(Arguments, compilation, out errors))
//.........这里部分代码省略.........
示例5: GetDependentTypesInProjectAsync
private static async Task GetDependentTypesInProjectAsync(
INamedTypeSymbol type, Project project, Solution solution, Func<INamedTypeSymbol, INamedTypeSymbol, bool> predicate, ConditionalWeakTable<Compilation, ConcurrentDictionary<SymbolKey, List<SymbolKey>>> cache, bool locationsInMetadata, ConcurrentSet<ISymbol> results, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var typeId = type.GetSymbolKey();
List<SymbolKey> dependentTypeIds;
if (!TryGetDependentTypes(cache, compilation, typeId, out dependentTypeIds))
{
List<INamedTypeSymbol> allTypes;
if (locationsInMetadata)
{
// From metadata, have to check other (non private) metadata types, as well as
// source types.
allTypes = GetAllSourceAndAccessibleTypesInCompilation(compilation, cancellationToken);
}
else
{
// It's from source, so only other source types could derive from it.
allTypes = GetAllSourceTypesInCompilation(compilation, cancellationToken);
}
dependentTypeIds = new List<SymbolKey>();
foreach (var t in allTypes)
{
cancellationToken.ThrowIfCancellationRequested();
if (predicate(t, type))
{
dependentTypeIds.Add(t.GetSymbolKey());
}
}
dependentTypeIds = GetOrAddDependentTypes(cache, compilation, typeId, dependentTypeIds);
}
foreach (var id in dependentTypeIds)
{
cancellationToken.ThrowIfCancellationRequested();
var resolvedSymbols = id.Resolve(compilation, cancellationToken: cancellationToken).GetAllSymbols();
foreach (var resolvedSymbol in resolvedSymbols)
{
var mappedSymbol = await SymbolFinder.FindSourceDefinitionAsync(resolvedSymbol, solution, cancellationToken).ConfigureAwait(false) ?? resolvedSymbol;
results.Add(mappedSymbol);
}
}
}
示例6: DetermineAllSymbolsCoreAsync
private async Task DetermineAllSymbolsCoreAsync(
SymbolAndProjectId symbolAndProjectId, ConcurrentSet<SymbolAndProjectId> result)
{
_cancellationToken.ThrowIfCancellationRequested();
var searchSymbolAndProjectId = MapToAppropriateSymbol(symbolAndProjectId);
// 2) Try to map this back to source symbol if this was a metadata symbol.
var sourceSymbolAndProjectId = await SymbolFinder.FindSourceDefinitionAsync(searchSymbolAndProjectId, _solution, _cancellationToken).ConfigureAwait(false);
if (sourceSymbolAndProjectId.Symbol != null)
{
searchSymbolAndProjectId = sourceSymbolAndProjectId;
}
var searchSymbol = searchSymbolAndProjectId.Symbol;
if (searchSymbol != null && result.Add(searchSymbolAndProjectId))
{
await _progress.OnDefinitionFoundAsync(searchSymbolAndProjectId).ConfigureAwait(false);
// get project to search
var projects = GetProjectScope();
_cancellationToken.ThrowIfCancellationRequested();
List<Task> finderTasks = new List<Task>();
foreach (var f in _finders)
{
finderTasks.Add(Task.Run(async () =>
{
var symbolTasks = new List<Task>();
var symbols = await f.DetermineCascadedSymbolsAsync(
searchSymbolAndProjectId, _solution, projects, _cancellationToken).ConfigureAwait(false);
AddSymbolTasks(result, symbols, symbolTasks);
// Defer to the language to see if it wants to cascade here in some special way.
var symbolProject = _solution.GetProject(searchSymbol.ContainingAssembly);
var service = symbolProject?.LanguageServices.GetService<ILanguageServiceReferenceFinder>();
if (service != null)
{
symbols = await service.DetermineCascadedSymbolsAsync(
searchSymbolAndProjectId, symbolProject, _cancellationToken).ConfigureAwait(false);
AddSymbolTasks(result, symbols, symbolTasks);
}
_cancellationToken.ThrowIfCancellationRequested();
await Task.WhenAll(symbolTasks).ConfigureAwait(false);
}, _cancellationToken));
}
await Task.WhenAll(finderTasks).ConfigureAwait(false);
}
}
示例7: GetPrefetcherFor
private PrefetchingBehavior GetPrefetcherFor(Etag fromEtag, ConcurrentSet<PrefetchingBehavior> usedPrefetchers)
{
foreach (var prefetchingBehavior in prefetchingBehaviors)
{
if (prefetchingBehavior.CanUsePrefetcherToLoadFrom(fromEtag) && usedPrefetchers.TryAdd(prefetchingBehavior))
return prefetchingBehavior;
}
var newPrefetcher = Database.Prefetcher.CreatePrefetchingBehavior(PrefetchingUser.SqlReplicator, null, "SqlReplication");
prefetchingBehaviors.Add(newPrefetcher);
usedPrefetchers.Add(newPrefetcher);
return newPrefetcher;
}
示例8: RunCore
internal int RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, CancellationToken cancellationToken)
{
Debug.Assert(!Arguments.IsScriptRunner);
cancellationToken.ThrowIfCancellationRequested();
if (Arguments.DisplayVersion)
{
PrintVersion(consoleOutput);
return Succeeded;
}
if (Arguments.DisplayLogo)
{
PrintLogo(consoleOutput);
}
if (Arguments.DisplayHelp)
{
PrintHelp(consoleOutput);
return Succeeded;
}
if (ReportErrors(Arguments.Errors, consoleOutput, errorLogger))
{
return Failed;
}
var touchedFilesLogger = (Arguments.TouchedFilesPath != null) ? new TouchedFileLogger() : null;
Compilation compilation = CreateCompilation(consoleOutput, touchedFilesLogger, errorLogger);
if (compilation == null)
{
return Failed;
}
var diagnosticInfos = new List<DiagnosticInfo>();
ImmutableArray<DiagnosticAnalyzer> analyzers = ResolveAnalyzersFromArguments(diagnosticInfos, MessageProvider);
var additionalTextFiles = ResolveAdditionalFilesFromArguments(diagnosticInfos, MessageProvider, touchedFilesLogger);
if (ReportErrors(diagnosticInfos, consoleOutput, errorLogger))
{
return Failed;
}
var diagnostics = new List<Diagnostic>();
ImmutableArray<EmbeddedText> embeddedTexts = AcquireEmbeddedTexts(compilation, diagnostics);
if (ReportErrors(diagnostics, consoleOutput, errorLogger))
{
return Failed;
}
bool reportAnalyzer = false;
CancellationTokenSource analyzerCts = null;
AnalyzerManager analyzerManager = null;
AnalyzerDriver analyzerDriver = null;
try
{
// Print the diagnostics produced during the parsing stage and exit if there were any errors.
if (ReportErrors(compilation.GetParseDiagnostics(), consoleOutput, errorLogger))
{
return Failed;
}
ConcurrentSet<Diagnostic> analyzerExceptionDiagnostics = null;
if (!analyzers.IsEmpty)
{
analyzerCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
analyzerManager = new AnalyzerManager();
analyzerExceptionDiagnostics = new ConcurrentSet<Diagnostic>();
Action<Diagnostic> addExceptionDiagnostic = diagnostic => analyzerExceptionDiagnostics.Add(diagnostic);
var analyzerOptions = new AnalyzerOptions(ImmutableArray<AdditionalText>.CastUp(additionalTextFiles));
analyzerDriver = AnalyzerDriver.CreateAndAttachToCompilation(compilation, analyzers, analyzerOptions, analyzerManager, addExceptionDiagnostic, Arguments.ReportAnalyzer, out compilation, analyzerCts.Token);
reportAnalyzer = Arguments.ReportAnalyzer && !analyzers.IsEmpty;
}
if (ReportErrors(compilation.GetDeclarationDiagnostics(), consoleOutput, errorLogger))
{
return Failed;
}
cancellationToken.ThrowIfCancellationRequested();
string outputName = GetOutputFileName(compilation, cancellationToken);
var finalPeFilePath = Path.Combine(Arguments.OutputDirectory, outputName);
var finalPdbFilePath = Arguments.PdbPath ?? Path.ChangeExtension(finalPeFilePath, ".pdb");
var finalXmlFilePath = Arguments.DocumentationPath;
var diagnosticBag = DiagnosticBag.GetInstance();
Stream sourceLinkStreamOpt = null;
try
{
// NOTE: Unlike the PDB path, the XML doc path is not embedded in the assembly, so we don't need to pass it to emit.
var emitOptions = Arguments.EmitOptions.
WithOutputNameOverride(outputName).
WithPdbFilePath(finalPdbFilePath);
//.........这里部分代码省略.........
示例9: GetPrefetcherFor
private PrefetchingBehavior GetPrefetcherFor(Etag fromEtag, ConcurrentSet<PrefetchingBehavior> usedPrefetchers)
{
foreach (var prefetchingBehavior in prefetchingBehaviors)
{
// at this point we've already verified that we can't use the default prefetcher
// if it's empty, we don't need to use it
if (prefetchingBehavior.IsDefault == false && prefetchingBehavior.IsEmpty() && usedPrefetchers.TryAdd(prefetchingBehavior))
return prefetchingBehavior;
}
var newPrefetcher = prefetcher.CreatePrefetchingBehavior(PrefetchingUser.Indexer, autoTuner, string.Format("Etags from: {0}", fromEtag));
prefetchingBehaviors.Add(newPrefetcher);
usedPrefetchers.Add(newPrefetcher);
return newPrefetcher;
}
示例10: GetPrefetcherFor
private PrefetchingBehavior GetPrefetcherFor(Etag fromEtag, ConcurrentSet<PrefetchingBehavior> usedPrefetchers)
{
foreach (var prefetchingBehavior in prefetchingBehaviors)
{
if (prefetchingBehavior.CanUsePrefetcherToLoadFrom(fromEtag) && usedPrefetchers.TryAdd(prefetchingBehavior))
return prefetchingBehavior;
}
var newPrefetcher = prefetcher.CreatePrefetchingBehavior(PrefetchingUser.Indexer, autoTuner,string.Format("Etags from: {0}", fromEtag));
var recentEtag = Etag.Empty;
context.Database.TransactionalStorage.Batch(accessor =>
{
recentEtag = accessor.Staleness.GetMostRecentDocumentEtag();
});
if (recentEtag.Restarts != fromEtag.Restarts || Math.Abs(recentEtag.Changes - fromEtag.Changes) > context.CurrentNumberOfItemsToIndexInSingleBatch)
{
// If the distance between etag of a recent document in db and etag to index from is greater than NumberOfItemsToProcessInSingleBatch
// then prevent the prefetcher from loading newly added documents. For such prefetcher we will relay only on future batches to prefetch docs to avoid
// large memory consumption by in-memory prefetching queue that would hold all the new documents, but it would be a long time before we can reach them.
newPrefetcher.DisableCollectingDocumentsAfterCommit = true;
}
prefetchingBehaviors.Add(newPrefetcher);
usedPrefetchers.Add(newPrefetcher);
return newPrefetcher;
}
示例11: ResolveAsync
private static async Task<ISymbol> ResolveAsync(
Document document, DeclaredSymbolInfo info, ConcurrentSet<SemanticModel> cachedModels,
CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
cachedModels.Add(semanticModel);
return info.Resolve(semanticModel, cancellationToken);
}
示例12: FindImmediatelyInheritingTypesInDocumentAsync
private static async Task<IEnumerable<INamedTypeSymbol>> FindImmediatelyInheritingTypesInDocumentAsync(
Document document,
HashSet<INamedTypeSymbol> typesToSearchFor,
InheritanceQuery inheritanceQuery,
ConcurrentSet<SemanticModel> cachedModels,
ConcurrentSet<IDeclarationInfo> cachedInfos,
Func<HashSet<INamedTypeSymbol>, INamedTypeSymbol, bool> typeImmediatelyMatches,
CancellationToken cancellationToken)
{
var declarationInfo = await document.GetDeclarationInfoAsync(cancellationToken).ConfigureAwait(false);
cachedInfos.Add(declarationInfo);
HashSet<INamedTypeSymbol> result = null;
foreach (var symbolInfo in declarationInfo.DeclaredSymbolInfos)
{
result = await ProcessSymbolInfo(
document, symbolInfo,
typesToSearchFor,
inheritanceQuery, cachedModels,
typeImmediatelyMatches, result, cancellationToken).ConfigureAwait(false);
}
return result;
}
示例13: GetPrefetcherFor
private PrefetchingBehavior GetPrefetcherFor(Etag fromEtag, ConcurrentSet<PrefetchingBehavior> usedPrefetchers)
{
foreach (var prefetchingBehavior in prefetchingBehaviors)
{
if (prefetchingBehavior.CanUsePrefetcherToLoadFrom(fromEtag) && usedPrefetchers.TryAdd(prefetchingBehavior))
return prefetchingBehavior;
}
var newPrefetcher = prefetcher.CreatePrefetchingBehavior(PrefetchingUser.Indexer, autoTuner);
prefetchingBehaviors.Add(newPrefetcher);
usedPrefetchers.Add(newPrefetcher);
return newPrefetcher;
}
示例14: FindImmediatelyInheritingTypesInDocumentAsync
private static async Task<ImmutableArray<SymbolAndProjectId<INamedTypeSymbol>>> FindImmediatelyInheritingTypesInDocumentAsync(
Document document,
SymbolAndProjectIdSet typesToSearchFor,
InheritanceQuery inheritanceQuery,
ConcurrentSet<SemanticModel> cachedModels,
ConcurrentSet<IDeclarationInfo> cachedInfos,
Func<SymbolAndProjectIdSet, INamedTypeSymbol, bool> typeImmediatelyMatches,
CancellationToken cancellationToken)
{
var declarationInfo = await document.GetDeclarationInfoAsync(cancellationToken).ConfigureAwait(false);
cachedInfos.Add(declarationInfo);
var result = CreateSymbolAndProjectIdSet();
foreach (var symbolInfo in declarationInfo.DeclaredSymbolInfos)
{
await ProcessSymbolInfo(
document, symbolInfo,
typesToSearchFor,
inheritanceQuery, cachedModels,
typeImmediatelyMatches, result, cancellationToken).ConfigureAwait(false);
}
return ToImmutableAndFree(result);
}