本文整理汇总了C#中ConcurrentBag.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.Contains方法的具体用法?C# ConcurrentBag.Contains怎么用?C# ConcurrentBag.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.Contains方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DescendFileSystem
/// <summary>
/// Walks the directory tree looking for the filename
/// </summary>
/// <param name="filename">the file to search for</param>
/// <param name="checkedPaths">the list of paths that have already been checked</param>
/// <param name="current">the current path that we are on</param>
/// <returns>the path to the filename if it is found</returns>
public static string DescendFileSystem(string filename, ConcurrentBag<string> checkedPaths, string current)
{
if (checkedPaths.Contains(current) || current.Contains("$RECYCLE.BIN"))
{
return string.Empty;
}
var file = Path.Combine(current, filename);
if (File.Exists(file))
{
return file;
}
checkedPaths.Add(current);
try
{
var paths = Directory.GetDirectories(current);
foreach (string path in paths)
{
var result = DescendFileSystem(filename, checkedPaths, path);
if (!string.IsNullOrEmpty(result))
{
return result;
}
}
}
catch (UnauthorizedAccessException)
{
return string.Empty;
}
return string.Empty;
}
示例2: MultipleInsertTest
public void MultipleInsertTest()
{
var modelReferences = new ConcurrentBag<IModelReference>();
for (int i = 0; i < 1000; i++)
{
var modelReference = this.TrackDao.InsertTrack(new TrackData("isrc", "artist", "title", "album", 2012, 200)
{
GroupId = "group-id"
});
Assert.IsFalse(modelReferences.Contains(modelReference));
modelReferences.Add(modelReference);
}
}
示例3: ShouldInsertMultipleTracksConcurrently
public void ShouldInsertMultipleTracksConcurrently()
{
const int NumberOfTracks = 1000;
var modelReferences = new ConcurrentBag<IModelReference>();
for (int i = 0; i < NumberOfTracks; i++)
{
var modelReference =
trackDao.InsertTrack(
new TrackData("isrc", "artist", "title", "album", 2012, 200) { GroupId = "group-id" });
Assert.IsFalse(modelReferences.Contains(modelReference));
modelReferences.Add(modelReference);
}
Assert.AreEqual(NumberOfTracks, trackDao.ReadAll().Count);
}
示例4: AddTypeToWorkList
/// <summary>
/// Adds the type to work list.
/// </summary>
/// <param name="messageType">Type of the message.</param>
private void AddTypeToWorkList(Type messageType)
{
ConcurrentBag<Type> listout;
if (!_workList.TryGetValue(messageType, out listout))
{
foreach (var pair in _handlerList)
{
if (pair.Key.IsAssignableFrom(messageType))
{
var list = new ConcurrentBag<Type>();
list = _workList.GetOrAdd(messageType, list);
Parallel.ForEach(pair.Value, t =>
{
_monitor.WaitOne();
if (!list.Contains(t))
{
list.Add(t);
}
_monitor.Set();
});
}
}
}
}
示例5: LoadUsersSequentialAsync
public async Task LoadUsersSequentialAsync(List<string> userIds)
{
bool retval = true;
ConcurrentBag<string> usersFailedToLoad = new ConcurrentBag<string>();
List<Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser>>> userLoadTasks = new
List<Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser>>>();
Dictionary<int, string> loadTaskIds = new Dictionary<int, string>();
DateTime start = DateTime.Now;
foreach (string userId in userIds)
{
Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser>> loadTask = connector.GetFrameworkUserAsync(userId, true);
userLoadTasks.Add(loadTask);
loadTaskIds.Add(loadTask.Id, userId);
}
try
{
await Task.WhenAll(userLoadTasks).ConfigureAwait(false);
}
catch (Exception e)
{
int nbCompleted = userLoadTasks.Where(t => t.IsCompleted).Count();
int nbFaulted = userLoadTasks.Where(t => t.IsFaulted).Count();
log("Exception when loading framework users from server " + ServerURL + ": " + e.Message + ", nb completed " + nbCompleted + " nb faulted "
+ nbFaulted, 2);
StringBuilder sb = new StringBuilder();
if (nbFaulted > 0)
sb.Append("Dumping errors of faulted tasks: ");
foreach (Task t in userLoadTasks.Where(t => t.IsFaulted))
{
sb.Append("User load task for user " + loadTaskIds[t.Id] + " failed: ");
foreach (Exception x in t.Exception.Flatten().InnerExceptions)
{
sb.Append(x.Message + " at " + x.StackTrace);
sb.Append(Environment.NewLine);
}
}
if (sb.Length > 0)
log(sb.ToString(), 2);
}
log("Waiting for user load tasks complete, duration " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms", 4);
List<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser> usersToLoadOts = new List<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser>();
foreach (Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser>> loadTask in userLoadTasks.Where(t => t.IsCompleted))
{
AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser> userRes = loadTask.Result;
if (!userRes.Success)
{
string userId = loadTaskIds[loadTask.Id];
log("Unable to extract user " + userId + " from server, " + ServerURL, 2);
usersFailedToLoad.Add(userId);
retval = false;
}
else
{
if (connector.HasOtsLine(userRes.ResultObject))
{
usersToLoadOts.Add(userRes.ResultObject);
}
}
}
if (userLoadTasks.Where(t => !t.IsCompleted).Count() > 0)
retval = false;
List<Task> dataLoadTasks = new List<Task>();
List<Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcOtsAccount>>> otsLoadTasks =
new List<Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcOtsAccount>>>();
List<Task<AlcPhoneOperationResult<VoiceMailInfo>>> vmLoadTasks = new List<Task<AlcPhoneOperationResult<VoiceMailInfo>>>();
Dictionary<int, string> otsLoadTaskIds = new Dictionary<int, string>();
Dictionary<int, string> vmLoadTaskIds = new Dictionary<int, string>();
foreach (AlcatelXmlApi6.AlcFwManagement.AlcFrameworkUser user in usersToLoadOts)
{
Task<AlcFrameworkUserOperationResult<AlcatelXmlApi6.AlcFwManagement.AlcOtsAccount>> otsTask =
connector.GetOtsAccountAsync(user.companyContacts.officePhone, true);
dataLoadTasks.Add(otsTask);
otsLoadTaskIds.Add(otsTask.Id, user.loginName);
otsLoadTasks.Add(otsTask);
Task<AlcPhoneOperationResult<VoiceMailInfo>> vmTask = connector.GetVoiceMailInfoAsync(user.companyContacts.officePhone);
dataLoadTasks.Add(vmTask);
vmLoadTaskIds.Add(vmTask.Id, user.loginName);
vmLoadTasks.Add(vmTask);
}
try
{
await Task.WhenAll(dataLoadTasks).ConfigureAwait(false);
}
catch (Exception e)
{
int nbCompleted = dataLoadTasks.Where(t => t.IsCompleted).Count();
int nbFaulted = dataLoadTasks.Where(t => t.IsFaulted).Count();
log("Exception when loading ots data / vm data from server " + ServerURL + ": " + e.Message + ", nb completed " + nbCompleted + " nb faulted "
+ nbFaulted, 2);
StringBuilder sb = new StringBuilder();
if (nbFaulted > 0)
sb.Append("Dumping errors of faulted tasks: ");
foreach (Task t in dataLoadTasks.Where(t => t.IsFaulted))
{
if (otsLoadTaskIds.ContainsKey(t.Id))
{
sb.Append("ots load task for user " + otsLoadTaskIds[t.Id] + " failed: ");
//.........这里部分代码省略.........
示例6: ShouldCatchAndHandleExceptionsThrownByEndpointObservables
public void ShouldCatchAndHandleExceptionsThrownByEndpointObservables()
{
FakeServiceEndpoint erroringEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage1)) { ThrowException = true };
FakeServiceEndpoint serviceEndpoint = new FakeServiceEndpoint(typeof(ITestServiceMessage2));
IServiceBus serviceBus = ServiceBus.Configure()
.WithEndpoint((IServiceEndpointClient) erroringEndpoint)
.WithEndpoint((IServiceEndpoint)erroringEndpoint)
.WithEndpoint((IServiceEndpointClient) serviceEndpoint)
.WithEndpoint((IServiceEndpoint)serviceEndpoint)
.UsingConsoleLogging().Create();
ConcurrentBag<IMessage> messages = new ConcurrentBag<IMessage>();
ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();
serviceBus.Events.Subscribe(messages.Add);
serviceBus.Commands.Subscribe(messages.Add);
serviceBus.Requests.Subscribe(messages.Add);
serviceBus.Exceptions.Subscribe(exceptions.Add);
// trigger exception
serviceBus.PublishAsync(new TestServiceEvent1());
TestServiceEvent2 message1 = new TestServiceEvent2();
serviceBus.PublishAsync(message1);
// trigger another exception
serviceBus.PublishAsync(new TestServiceEvent1());
TestServiceEvent2 message2 = new TestServiceEvent2();
serviceBus.PublishAsync(message2);
Assert.That(exceptions.Count(), Is.EqualTo(2));
Assert.That(messages.Contains(message1), "message1 not received");
Assert.That(messages.Contains(message2), "message2 not received");
}
示例7: Contains
public void Contains()
{
var TestObject = new ConcurrentBag<int>(new int[] { 1, 2, 3, 4, 5, 6 });
Assert.True(TestObject.Contains(4));
Assert.False(TestObject.Contains(-1));
}
示例8: GroupAS
// TODO: Move above into DetectedProc class methods
/// <summary>
/// Group address spaces into related buckets
///
/// We will assign an address space ID to each detected proc so we know what process belongs with who
/// After AS grouping we will know what EPTP belongs to which AS since one of the DP's will have it's CR3 in the VMCS
///
/// Yes it's a bit complicated.
///
/// The overall procedure however is straight forward in that;
///
/// * For every detected process
/// Bucket into groups which are the "Address spaces" that initially are
///
/// (a) based on kernel address space similarities
/// and then
/// (b) based on what VMCS value was found pointing to that group
///
/// This ensures that if we have several hypervisors with a possibly identical kernel grouping (i.e. the PFN's
/// were used by each kernel were identical), they are disambiguated by the VMCS. (Which can be validated later)
///
/// The benefit here is that brute forcing at this stage is fairly expensive and can lead to significant overhead, there does
/// tend to be some outliers for large systems that need to be looked at more to determine who they belong too. Nevertheless, it's
/// inconsequential if they are grouped with the appropriate AS since even if they are isolated into their own 'AS' this is an artificial
/// construct for our book keeping. The net result is that even if some process is grouped by itself due to some aggressive variation in
/// kernel PFN' use (lots of dual mapped memory/MDL's or something), it's still able to be dumped and analyzed.
///
///
/// </summary>
/// <param name="pTypes">Types to scan for, this is of the already detected processes list so it's already filtered really</param>
public void GroupAS(PTType pTypes = PTType.UNCONFIGURED)
{
var PT2Scan = pTypes == PTType.UNCONFIGURED ? PTType.ALL : pTypes;
//if (Phase >=3 && OverRidePhase)
// return;
// To join an AS group we want to see > 50% correlation which is a lot considering were only interoperating roughly 10-20 values (more like 12)
var p = from proc in Processes
where (((proc.PageTableType & PT2Scan) == proc.PageTableType))
orderby proc.CR3Value ascending
select proc;
ASGroups = new ConcurrentDictionary<int, ConcurrentBag<DetectedProc>>();
// we trim out the known recursive/self entries since they will naturally not be equivalent
var AlikelyKernelSet = from ptes in p.First().TopPageTablePage
where ptes.Key > 255 && MagicNumbers.Each.All(ppx => ppx != ptes.Key)
select ptes.Value;
int totUngrouped = Processes.Count();
int CurrASID = 1;
int LastGroupTotal = 0;
var grouped = new ConcurrentBag<DetectedProc>();
WriteLine($"Scanning for group correlations total processes {totUngrouped}");
ASGroups[CurrASID] = new ConcurrentBag<DetectedProc>();
while (true)
{
ForegroundColor = ConsoleColor.Yellow;
Parallel.ForEach(p, (proc) =>
{
var currKern = from ptes in proc.TopPageTablePage
where ptes.Key > 255 && MagicNumbers.Each.All(ppx => ppx != ptes.Key)
select ptes.Value;
var interSection = currKern.Intersect(AlikelyKernelSet);
var correlated = interSection.Count() * 1.00 / AlikelyKernelSet.Count();
// add this detected CR3/process address space to an address space grouping when
// the kernel range is above the acceptable threshold, the group does not contain this proc
// and this proc is not already joined into another group
if (correlated > GroupThreshold && !ASGroups[CurrASID].Contains(proc) && proc.AddressSpaceID == 0)
{
WriteColor(ConsoleColor.Cyan, $"MemberProces: Group {CurrASID} Type [{proc.PageTableType}] GroupCorrelation [{correlated:P3}] PID [{proc.CR3Value:X}]");
proc.AddressSpaceID = CurrASID;
ASGroups[CurrASID].Add(proc);
// global list to quickly scan
grouped.Add(proc);
}
});
ForegroundColor = ConsoleColor.Yellow;
var totGrouped = (from g in ASGroups.Values
select g).Sum(x => x.Count());
WriteLine($"Finished Group {CurrASID} collected size {ASGroups[CurrASID].Count()}, continuing to group");
// if there is more work todo, setup an entry for testing
if (totGrouped < totUngrouped)
{
// if we wind up here
// there has been no forward progress in isolating further groups
if(LastGroupTotal == totGrouped)
{
ForegroundColor = ConsoleColor.Red;
WriteLine($"Terminating with non-grouped process candidates. GroupThreshold may be too high. {GroupThreshold}");
//.........这里部分代码省略.........
示例9: Run
public async Task<List<TimeSpan>> Run()
{
var results = new List<TimeSpan>();
for (var n = 0; n < NoOfIterations; n++)
{
DateTime start = DateTime.Now;
var proposers = new ConcurrentBag<Proposer>();
var acceptors = new ConcurrentBag<Acceptor>();
var proposedValues = new ConcurrentBag<string>();
var acceptedValues = new ConcurrentBag<Task<string>>();
for (int i = 0; i < Proposers; i++)
{
proposers.Add(new Proposer(Guid.NewGuid().ToString(), Acceptors));
}
for (int i = 0; i < LiveAcceptors; i++)
{
acceptors.Add(new Acceptor("Address"));
}
foreach (var proposer in proposers)
foreach (var acceptor in acceptors)
proposer.Pipe(new Delay<NetworkMessage>(TimeSpan.FromMilliseconds(minDelay), TimeSpan.FromMilliseconds(maxDelay)))
.Pipe(new Drop<NetworkMessage>(drop))
.Pipe(acceptor)
.Pipe(new Delay<NetworkMessage>(TimeSpan.FromMilliseconds(minDelay), TimeSpan.FromMilliseconds(maxDelay)))
.Pipe(new Drop<NetworkMessage>(drop))
.Pipe(proposer);
foreach (var proposer in proposers)
{
acceptedValues.Add(Task.Factory.StartNew(() =>
{
var val = Guid.NewGuid().ToString();
proposedValues.Add(val);
return proposer.Propose(val);
})
.Unwrap());
}
var acceptedValue = await acceptedValues.First();
foreach (var res in acceptedValues)
{
var result = await res;
if (result != acceptedValue) throw new Exception("The proposers did not all get the same result");
if (!proposedValues.Contains(result)) throw new Exception("The accepted Value was never proposed");
}
DateTime end = DateTime.Now;
results.Add(end.Subtract(start));
}
return results;
}
示例10: CheckQueueMessage
private static void CheckQueueMessage(string message1, ConcurrentBag<string> recievedLogsGetParam1)
{
var success = recievedLogsGetParam1.Contains(message1);
Assert.True(success, string.Format("message '{0}' not found", message1));
}
示例11: ReadPackagesFromDisk
/// <summary>
/// ReadPackagesFromDisk loads all packages from disk and determines additional metadata such as the hash, IsAbsoluteLatestVersion, and IsLatestVersion.
/// </summary>
private HashSet<ServerPackage> ReadPackagesFromDisk()
{
_logger.Log(LogLevel.Info, "Start reading packages from disk...");
MonitorFileSystem(false);
try
{
var cachedPackages = new ConcurrentBag<ServerPackage>();
bool enableDelisting = EnableDelisting;
var packages = _expandedPackageRepository.GetPackages().ToList();
Parallel.ForEach(packages, package =>
{
// Create server package
var serverPackage = CreateServerPackage(package, enableDelisting);
// Add the package to the cache, it should not exist already
if (cachedPackages.Contains(serverPackage))
{
_logger.Log(LogLevel.Warning, "Duplicate package found - {0} {1}", package.Id, package.Version);
}
else
{
cachedPackages.Add(serverPackage);
}
});
_logger.Log(LogLevel.Info, "Finished reading packages from disk.");
return new HashSet<ServerPackage>(cachedPackages, PackageEqualityComparer.IdAndVersion);
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, "Error while reading packages from disk: {0} {1}", ex.Message, ex.StackTrace);
throw;
}
finally
{
MonitorFileSystem(true);
}
}