public bool TryCopyFrom(FileStream fileStream, out int readed) { lock (obj) { int bufferIndex; if (readQueue.TryPeek(out bufferIndex)) { buffer[bufferIndex].Read(fileStream, out readed); if (readed > 0) { readQueue.TryDequeue(out bufferIndex); writeQueue.Enqueue(bufferIndex); ProgressChanged?.Invoke(readQueue.Count() * 100 / buffer.Length); return(true); } return(false); } else { readed = 0; return(false); } } }
// Handle when Mining Level Changes public void mineLevelChanged(object sender, WarpedEventArgs e) { //BNC_Core.Logger.Log("Warped", LogLevel.Info); if (e.NewLocation is MineShaft mine) { if (shouldUpdateAugment(e)) //if (true) { if (TwitchIntergration.isConnected()) { //BNC_Core.Logger.Log("connected", LogLevel.Info); if (!TwitchIntergration.hasAugPollStarted) { TwitchIntergration.StartAugPoll(getRandomBuff(3, true)); } } else { BaseAugment aug = getRandomBuff(1)[0]; aug.isMineOnly = true; AddAugmentQueue(aug); Game1.addHUDMessage(new HUDMessage(aug.DisplayName + ": " + aug.desc, null)); } } } if (_augmentQueue.Count() > 0) { UpdateLocation(e); } }
public void Read(INode node) { var guid = Guid.NewGuid().GetHashCode(); // read while (true) { Thread.Sleep(1000); if (queue.Count() > 10) { Console.WriteLine("read too more,count: " + queue.Count()); continue; } if (node.IsStopped) { Console.WriteLine("return read when IsStopped"); return; } var rand = new Random(guid); var list = new List <int>(); list.Add(rand.Next(100)); list.Add(rand.Next(100)); list.Add(rand.Next(100)); queue.Enqueue(list); Console.WriteLine("read: " + string.Join(",", list)); } }
public static void TestEtw() { RemoteInvoke(() => { using (var listener = new TestEventListener(new Guid("159eeeec-4a14-4418-a8fe-faabcd987887"), EventLevel.Verbose)) { var events = new ConcurrentQueue <int>(); listener.RunWithCallback(ev => events.Enqueue(ev.EventId), () => { Enumerable.Range(0, 10000).AsParallel().Select(i => i).ToArray(); }); const int BeginEventId = 1; Assert.Equal(expected: 1, actual: events.Count(i => i == BeginEventId)); const int EndEventId = 2; Assert.Equal(expected: 1, actual: events.Count(i => i == EndEventId)); const int ForkEventId = 3; const int JoinEventId = 4; Assert.True(events.Count(i => i == ForkEventId) > 0); Assert.Equal(events.Count(i => i == ForkEventId), events.Count(i => i == JoinEventId)); } }).Dispose(); }
public void GetHostEntry_ValidName_NoErrors() { using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose)) { var events = new ConcurrentQueue <EventWrittenEventArgs>(); listener.RunWithCallback(ev => events.Enqueue(ev), () => { try { Dns.GetHostEntryAsync("localhost").GetAwaiter().GetResult(); Dns.GetHostEntryAsync(IPAddress.Loopback).GetAwaiter().GetResult(); Dns.GetHostEntry("localhost"); Dns.GetHostEntry(IPAddress.Loopback); } catch (Exception e) { throw new SkipTestException($"Localhost lookup failed unexpectedly: {e.Message}"); } }); // We get some traces. Assert.True(events.Count() > 0); // No errors or warning for successful query. Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0); } }
public void ProcessQueue() { while (_queue.Count() > 0) { Thread.Sleep(1); } }
public void TestEtw() { using (var listener = new TestEventListener("System.Collections.Concurrent.ConcurrentCollectionsEventSource", EventLevel.Verbose)) { var events = new ConcurrentQueue <int>(); const int AcquiringAllLocksEventId = 3; Clear(events); listener.RunWithCallback(ev => events.Enqueue(ev.EventId), () => { var cd = new ConcurrentDictionary <int, int>(); cd.TryAdd(1, 1); cd.Clear(); }); Assert.True(events.Count(i => i == AcquiringAllLocksEventId) > 0); const int TryTakeStealsEventId = 4; const int TryPeekStealsEventId = 5; Clear(events); listener.RunWithCallback(ev => events.Enqueue(ev.EventId), () => { var cb = new ConcurrentBag <int>(); int item; cb.TryPeek(out item); cb.TryTake(out item); }); Assert.True(events.Count(i => i == TryPeekStealsEventId) > 0); Assert.True(events.Count(i => i == TryTakeStealsEventId) > 0); // No tests for: // CONCURRENTSTACK_FASTPUSHFAILED_ID // CONCURRENTSTACK_FASTPOPFAILED_ID // These require certain race condition interleavings in order to fire. } }
/// <summary> /// Writes the statistics of the execution report using <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The writer object.</param> private void WriteCounterStats(XmlWriter writer) { writer.WriteStartElement("CounterStatistics"); writer.WriteAttributeString("TotalEntries", _entries.Count.ToString()); writer.WriteAttributeString("FailedEntries", _entries.Count(e => e.Status == "Fail").ToString()); writer.WriteAttributeString("SuccessEntries", _entries.Count(e => e.Status == "Success").ToString()); writer.WriteAttributeString("SkippedEntries", _entries.Count(e => e.Status == "Skipped").ToString()); writer.WriteEndElement(); }
public virtual void SendMessage(IMessageServerChat message) { if (_messagesQueued.Count() > _maxMessagesInQueue) { _messagesQueued.TryDequeue(out _); } _messagesQueued.Enqueue(message); }
public static void Main(string[] args) { while (numberList.Count() < 1000000) { var program = new Program(3); program.Run(); _countdown.Wait(); Console.WriteLine(numberList.Count()); } Console.ReadLine(); }
private void timer_Tick(object sender, EventArgs e) { if (serCom.IsOpen) { txtBytesToRead.Text = serialBytesToReadDisplay.ToString(); txtDataStream.AppendText(serialDataString); txtTempStringLength.Text = serialDataString.Length.ToString(); txtItemsInQueue.Text = dataQueue.Count().ToString(); serialDataString = ""; while (dataQueue.Count() > 0) { if (dataQueue.TryDequeue(out int nextByte)) { if (nextByte == 255) { dataState = 1; } else if (dataState == 1) { dataState = 2; txtAx.Text = nextByte.ToString(); if (cbxSaveToFile.Checked == true) { outputFile.Write(nextByte.ToString() + ", "); } } else if (dataState == 2) { dataState = 3; txtAy.Text = nextByte.ToString(); if (cbxSaveToFile.Checked == true) { outputFile.Write(nextByte.ToString() + ", "); } } else if (dataState == 3) { dataState = 0; txtAz.Text = nextByte.ToString(); if (cbxSaveToFile.Checked == true) { outputFile.Write(nextByte.ToString() + ", " + DateTime.Now.ToLongTimeString() + "\n"); } } // serialDataString += dequeueItem.ToString() + ", "; } } } }
public static void ConcurrentTest() //多生产者,一个消费者 { _keysQueue = new ConcurrentQueue <string>(); _byteArraysQueue = new ConcurrentQueue <byte[]>(); var sw = Stopwatch.StartNew(); var taskAESKeys = Task.Factory.StartNew(() => ParallelPartitionGenerateAESKeys(Environment.ProcessorCount - 1)); var taskHexStrings = Task.Factory.StartNew(() => ConvertAESKeysToHex(taskAESKeys)); string lastKey; while (taskHexStrings.Status == TaskStatus.Running || taskHexStrings.Status == TaskStatus.WaitingToRun) { var countResult = _keysQueue.Count(key => key.Contains("F")); Console.WriteLine($"So far, the number of keys that contaion an F is: {countResult}"); if (_keysQueue.TryPeek(out lastKey)) { Console.WriteLine($"The first key in the queue is: {lastKey}"); } else { Console.WriteLine("No keys yet."); } System.Threading.Thread.Sleep(500); } Task.WaitAll(taskAESKeys, taskHexStrings); Console.WriteLine($"Number of keys in the list: {_keysQueue.Count}"); Console.WriteLine($"Time: {sw.Elapsed.ToString()}"); Console.WriteLine("Finished"); }
private void PushOutputDataEvents(DateTimeOffset outputTime, IEnumerable <KeyValuePair <IDAQOutputStream, IOutputData> > outgoingData) { var exceptions = new ConcurrentQueue <Exception>(); Parallel.ForEach(outgoingData, kvp => { try { var outputStream = kvp.Key; var data = (kvp.Value as IOutputData); if (data != null) { data = data.DataWithNodeConfiguration(this.Name, this.Configuration); Task.Factory.StartNew( () => OnStimulusOutput(outputTime, outputStream, data)); outputStream.DidOutputData(outputTime, data.Duration, data.Configuration); } } catch (Exception e) { exceptions.Enqueue(e); } }); if (exceptions.Count() > 0) { throw new AggregateException(exceptions); } }
static void Main(string[] args) { var sw = Stopwatch.StartNew(); _byteArraysQueue = new ConcurrentQueue <byte[]>(); _keysQueue = new ConcurrentQueue <string>(); int taskAESKeysMax = Environment.ProcessorCount / 2; // Use the remaining logical cores // to create parallelized tasks to run many consumers int taskHexStringsMax = Environment.ProcessorCount - taskAESKeysMax; var taskAESKeys = Task.Factory.StartNew(() => ParallelPartitionGenerateAESKeys(taskAESKeysMax)); Task[] tasksHexStrings = new Task[taskHexStringsMax]; for (int i = 0; i < taskHexStringsMax; i++) { tasksHexStrings[i] = Task.Factory.StartNew(() => ConvertAESKeysToHex(taskAESKeys)); } Task.WaitAll(tasksHexStrings); Console.WriteLine("Number of keys in the list: {0}", _keysQueue.Count()); Debug.WriteLine(sw.Elapsed.ToString()); // Display the results and wait for the user to press a key Console.WriteLine("Finished!"); Console.ReadLine(); }
public void AddDocument(T document, bool clearDocumentRefs = true) { if (document == null) { throw new Exception("Document is null"); } var doc = GetDocument(document, clearDocumentRefs); int temporyCount = 0; DocumentPartition partition; if (doc.Exists) { partition = new DocumentPartition(doc.Partition, doc.Exists); } else { if (IsPartialStore) { temporyCount = Queue.Count(d => d.PartOf.Partition == Meta.Partitions.Current); partition = Meta.GetPartition(Meta.Partitions.Current, PartialStoreLimit, temporyCount); } else { partition = new DocumentPartition(Meta.Partitions.Current, doc.Exists); } } Enqueue(doc, partition); }
public override IEnumerable <TestUnitResult> RunTestUnits(TaskFactory taskFactory, Task prepTask) { if (StartTime.Ticks == 0) { StartTime = DateTimeOffset.UtcNow; } if (prepTask.IsFaulted) { throw prepTask.Exception; } List <Task <TestUnitResult> > _testUnits = new List <Task <TestUnitResult> >(); Task[] previousTask = Array.ConvertAll(new int[target.TargetSiteConfig.MaxCatalogueThread], mp => prepTask); int i = queryFilters.Count(); while (i > 0) { for (int j = 0; j < target.TargetSiteConfig.MaxCatalogueThread; j++) { var _testUnit = previousTask[j].ContinueWith <KeyValuePair <IOpenSearchable, FiltersDefinition> >((task) => { prepTask.Wait(); FiltersDefinition randomFilter; queryFilters.TryDequeue(out randomFilter); return(new KeyValuePair <IOpenSearchable, FiltersDefinition>(target.CreateOpenSearchableEntity(randomFilter, Configuration.Current.Global.QueryTryNumber), randomFilter)); }). ContinueWith((request) => { if (request.Result.Value == null) { return(null); } return(MakeQuery(request.Result.Key, request.Result.Value)); }); _testUnits.Add(_testUnit); previousTask[j] = _testUnit; i--; } } try { Task.WaitAll(_testUnits.ToArray()); } catch (AggregateException ae) { Exception ex = ae; while (ex is AggregateException) { ex = ex.InnerException; } log.Debug(ex.Message); log.Debug(ex.StackTrace); } EndTime = DateTimeOffset.UtcNow; return(_testUnits.Select(t => t.Result).Where(r => r != null)); }
private void Listen() { while (true) { try { if (listener.IsListening) { var context = listener.GetContext(); if (queueListenerContext.Count() > requestBound) { SetListenerContextError(context, (int)HttpStatusCode.ServiceUnavailable); } else { queueListenerContext.Enqueue(Tuple.Create(context, DateTime.Now)); } } else { Thread.Sleep(0); } } catch (Exception) { return; } } }
public void ReturnAllocation(MemoryAllocation finishedWith) { //put back into pool _allocationPool.Enqueue(finishedWith); return; int count = _inputBuffer.Count(); if (count <= numberOfBlockInPool / 10) { return; } //Try to keep in order List <MemoryAllocation> itemsToReAdd = new List <MemoryAllocation>(); for (int i = 0; i < count; i++) { MemoryAllocation dequeuedItem; if (_inputBuffer.TryDequeue(out dequeuedItem)) { itemsToReAdd.Add(dequeuedItem); } } foreach (var memoryAllocation in itemsToReAdd.OrderBy(o => o.Offset)) { _allocationPool.Enqueue(memoryAllocation); } }
public void QuickPulseQuotaTrackerIsThreadSafe() { // ARRANGE int maxQuota = 100 * 60; int experimentLengthInSeconds = 1000; int concurrency = 1000; var mockTimeProvider = new ClockMock(); var quotaTracker = new QuickPulseQuotaTracker(mockTimeProvider, maxQuota, 0); var quotaApplicationResults = new ConcurrentQueue<bool>(); // ACT for (int i = 0; i < experimentLengthInSeconds; i++) { mockTimeProvider.FastForward(TimeSpan.FromSeconds(1)); var tasks = new List<Action>(); for (int j = 0; j < concurrency; j++) { tasks.Add(() => quotaApplicationResults.Enqueue(quotaTracker.ApplyQuota())); } Parallel.Invoke(new ParallelOptions() { MaxDegreeOfParallelism = concurrency }, tasks.ToArray()); } // ASSERT var passedQuotaCount = quotaApplicationResults.Count(result => result); var correctResult = maxQuota / 60 * experimentLengthInSeconds; Assert.AreEqual(correctResult, passedQuotaCount); Assert.IsFalse(quotaTracker.ApplyQuota()); }
public Session ConnectToPublicRoom(string login) { if (login == null) { throw new ArgumentNullException(); } var sessionCount = _connectionQueue.Count(); var iteration = 0; while (_connectionQueue.TryDequeue(out Session session) && iteration != sessionCount) { iteration++; if (session.IsFinished) { continue; } if (session.Player1Login == login) { _connectionQueue.Enqueue(session); continue; } session.AddSecondPlayer(login); return(session); } return(null); }
public void messageDoInBackground() { //run = true; Console.WriteLine("MessageTask: Message task started"); while (run) { if (inQueue.Count() >= 0) { string message; Boolean m = inQueue.TryDequeue(out message); Console.WriteLine("MessageTask: " + message); //string prefix = ""; /*if (message.CharAt(0) == ':') * { //indicates presense of a prefix * int spaceIndex = message.indexOf(' '); * prefix = message.substring(0, spaceIndex); * message = message.substring(spaceIndex); * }*/ int spaceIndex = message.IndexOf(' '); string command = message.Substring(0, spaceIndex).ToUpper(); if (command.Equals("PING")) { handlePing(); continue; } //publishProgress(prefix, message); } } //return run; }
private bool ShouldSendQueuedSpansOverWire() { return(serializableSpans.Any() && (serializableSpans.Count() >= maxBatchSize || spanProcessorTaskFactory.IsTaskCancelled() || subsequentPollCount > MAX_NUMBER_OF_POLLS)); }
[PlatformSpecific(~TestPlatforms.Windows)] // Unreliable on Windows. public void GetHostEntryAsync_InvalidHost_LogsError() { using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) { var events = new ConcurrentQueue <EventWrittenEventArgs>(); listener.RunWithCallback(ev => events.Enqueue(ev), () => { try { Dns.GetHostEntryAsync(Configuration.Sockets.InvalidHost).GetAwaiter().GetResult(); throw new SkipTestException("GetHostEntry() should fail but it did not."); } catch (SocketException e) when(e.SocketErrorCode == SocketError.HostNotFound) { } catch (Exception e) { throw new SkipTestException($"GetHostEntry failed unexpectedly: {e.Message}"); } }); Assert.True(events.Count() > 0); foreach (EventWrittenEventArgs ev in events) { Assert.True(ev.Payload.Count >= 3); Assert.NotNull(ev.Payload[0]); Assert.NotNull(ev.Payload[1]); Assert.NotNull(ev.Payload[2]); } } }
public async Task TestReadAException() { var list = Enumerable.Range(1, 1000).Select(x => x); var expectedResult = Enumerable.Range(1, 100).Select(x => x * 10); var exceptionList = new ConcurrentQueue <int>(); var myChannel = new ChannelHelper <int>(); var result = await myChannel.Source(list) .SetConsumer(5) .ReadAllAsync <int>(t => t / (t % 10), (i, e) => { exceptionList.Enqueue(i); }); var hash = new HashSet <int>(); foreach (var item in expectedResult) { hash.Add(item); } Assert.Equal(hash.Count(), exceptionList.Count()); foreach (var item in exceptionList) { Assert.Contains(item, hash); } }
public int CalculateNewRecommendedBatchSize(int numberActuallyRetrievedInLastBatch) { // This may race with the trimmer but the batch size is only adjusted by one so a collision has limited impact and it will self-correct in the next run anyway. var count = finishedWithinSafeLockTime.Count; if (count < 2) { return currentRecommendedBatchSize; } var countFinishedWithinTime = finishedWithinSafeLockTime.Count(finishedOnTime => finishedOnTime); var percentFinishedWithinTime = countFinishedWithinTime * 100 / count; if (percentFinishedWithinTime >= 95) { if (numberActuallyRetrievedInLastBatch >= currentRecommendedBatchSize) { currentRecommendedBatchSize++; logger.LogDebug($"Increasing batch size to {currentRecommendedBatchSize} as {percentFinishedWithinTime}% of the last {count} messages finished within the safe period and the queue appears to be backlogged."); } return currentRecommendedBatchSize; } if (currentRecommendedBatchSize > 1) { currentRecommendedBatchSize--; logger.LogDebug($"Decreasing batch size to {currentRecommendedBatchSize} as only {percentFinishedWithinTime}% of the last {count} messages finished within the safe period."); } return currentRecommendedBatchSize; }
public Task Add(string ip, ConcurrentBag <byte[]> data) { return(Task.Run(() => { //设置一个接收阀值 var queueCount = datas.Count(); if (queueCount < ReceivingThreshold) { var etwData = new EtwData { guid = Guid.NewGuid().ToString(), buffList = data, enumEtwStatus = EnumEtwStatus.None, time = DateTime.Now.Ticks, retryCount = 0, senderIp = ip, }; datas.Enqueue(etwData); } else { //logger.Debug(JsonConvert.SerializeObject( // $"EtwData Add more than max=[{ReceivingThreshold}]" //)); } })); }
private void LogQueueStatistics() { AppInsights.TelemetryClient.TrackEvent("StartLoggingQueueStats", null); var freeSitesCount = FreeResourceGroups.Count(sub => sub.SubscriptionType == SubscriptionType.AppService); var inUseSites = ResourceGroupsInUse.Select(s => s.Value).Where(sub => sub.SubscriptionType == SubscriptionType.AppService); var resourceGroups = inUseSites as IList <ResourceGroup> ?? inUseSites.ToList(); var inUseSitesCount = resourceGroups.Count(); var inUseFunctionsCount = resourceGroups.Count(res => res.AppService == AppService.Function); var inUseWebsitesCount = resourceGroups.Count(res => res.AppService == AppService.Web); var inUseContainerCount = resourceGroups.Count(res => res.AppService == AppService.Containers); var inUseLogicAppCount = resourceGroups.Count(res => res.AppService == AppService.Logic); var inUseApiAppCount = resourceGroups.Count(res => res.AppService == AppService.Api); var inProgress = ResourceGroupsInProgress.Select(s => s.Value).Count(); var backgroundOperations = BackgroundInternalOperations.Select(s => s.Value).Count(); var freeLinuxResources = FreeLinuxResourceGroups.Count(sub => sub.SubscriptionType == SubscriptionType.Linux); var inUseLinuxResources = ResourceGroupsInUse.Select(s => s.Value).Count(sub => sub.SubscriptionType == SubscriptionType.Linux); AppInsights.TelemetryClient.TrackMetric("freeSites", freeSitesCount); AppInsights.TelemetryClient.TrackMetric("inUseSites", inUseSitesCount); AppInsights.TelemetryClient.TrackMetric("inUseFunctionsCount", inUseFunctionsCount); AppInsights.TelemetryClient.TrackMetric("inUseWebsitesCount", inUseWebsitesCount); AppInsights.TelemetryClient.TrackMetric("inUseContainersCount", inUseContainerCount); AppInsights.TelemetryClient.TrackMetric("inUseLogicAppCount", inUseLogicAppCount); AppInsights.TelemetryClient.TrackMetric("inUseApiAppCount", inUseApiAppCount); AppInsights.TelemetryClient.TrackMetric("inUseSites", inUseSitesCount); AppInsights.TelemetryClient.TrackMetric("inProgressOperations", inProgress); AppInsights.TelemetryClient.TrackMetric("backgroundOperations", backgroundOperations); AppInsights.TelemetryClient.TrackMetric("freeLinuxResources", freeLinuxResources); AppInsights.TelemetryClient.TrackMetric("inUseLinuxResources", inUseLinuxResources); }
//循环扫面消息队列并发送消息 private void QuSendMsg() { while (true) { //当有需要发送的消息时 if (_conQuSendMsg.Count(e => e.NeedSendTime <= DateTime.Now) > 0) { int count = _conQuSendMsg.Count; for (int i = 0; i < count; i++) { TMsg sendMsg = default(TMsg); bool gotElement = _conQuSendMsg.TryDequeue(out sendMsg); if (!gotElement || sendMsg == null) { continue; } if (sendMsg.NeedSendTime > DateTime.Now) { _conQuSendMsg.Enqueue(sendMsg); continue; } OKSend(sendMsg); } } Thread.Sleep(MilsecTimeout); } }
private void GenerateWorkers() { monitor_stop = false; Task.Run(() => { while (!monitor_stop) { while (write_tasks.Count() > 0) { Task t = null; write_tasks.TryDequeue(out t); if (t != null) { t.Start(); t.Wait(); } } try { Refresh(); } catch (Exception ex) { FileTextLogger.logger.AppendLog("REM, GEN - A fatal error occured during expander state refresh. The operation has been aborted"); FileTextLogger.logger.AppendLog(ex.Message); } } }); }
public void DoesNotDispatchMessageWithNullBus() { var busInstances = new ConcurrentQueue<IBus>(); using (var activator = new BuiltinHandlerActivator()) { activator.Handle<string>(async (bus, message) => { busInstances.Enqueue(bus); }); Configure.With(activator) .Logging(l => l.ColoredConsole(LogLevel.Warn)) .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "race-condition")) .Options(o => { o.SetNumberOfWorkers(1); o.SetMaxParallelism(1); }) .Start(); Task.WaitAll(Enumerable.Range(0, 1000) .Select(i => activator.Bus.SendLocal($"message-{i}")) .ToArray()); } Thread.Sleep(1000); var numberOfNulls = busInstances.Count(i => i == null); Assert.That(numberOfNulls, Is.EqualTo(0), "Did not expect any messages to be dispatched with a NULL bus instance"); }
private void initQueue(string file1, string file2) { queue = new ConcurrentQueue <JToken[]>(); JToken fninfo1 = readJSON(file1)["functions"]; JToken fninfo2 = readJSON(file2)["functions"]; Console.WriteLine(file1 + " functions : " + fninfo1.Count()); Console.WriteLine(file2 + " functions : " + fninfo2.Count()); Console.WriteLine("#Queue is setting..."); DateTime start = DateTime.Now; for (int i = 0; i < fninfo1.Count(); i++) { for (int j = 0; j < fninfo2.Count(); j++) { if (this.isCandidate(fninfo1[i], fninfo2[j])) { JToken[] couple = { fninfo1[i], fninfo2[j] }; queue.Enqueue(couple); } } } DateTime end = DateTime.Now; Console.WriteLine("Filtering Time : " + (end - start)); Console.WriteLine("#Complete. queue size is " + queue.Count()); }
public void DoesNotDispatchMessageWithNullBus() { var busInstances = new ConcurrentQueue <IBus>(); using (var activator = new BuiltinHandlerActivator()) { activator.Handle <string>(async(bus, message) => { busInstances.Enqueue(bus); }); Configure.With(activator) .Logging(l => l.ColoredConsole(LogLevel.Warn)) .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "race-condition")) .Options(o => { o.SetNumberOfWorkers(1); o.SetMaxParallelism(1); }) .Start(); Task.WaitAll(Enumerable.Range(0, 1000) .Select(i => activator.Bus.SendLocal($"message-{i}")) .ToArray()); } Thread.Sleep(1000); var numberOfNulls = busInstances.Count(i => i == null); Assert.That(numberOfNulls, Is.EqualTo(0), "Did not expect any messages to be dispatched with a NULL bus instance"); }
public void TestEtw() { using (var listener = new TestEventListener("System.Threading.Tasks.Parallel.EventSource", EventLevel.Verbose)) { var events = new ConcurrentQueue<int>(); listener.RunWithCallback(ev => events.Enqueue(ev.EventId), () => { Parallel.For(0, 10000, i => { }); var barrier = new Barrier(2); Parallel.Invoke( () => barrier.SignalAndWait(), () => barrier.SignalAndWait()); }); const int BeginLoopEventId = 1; const int BeginInvokeEventId = 3; Assert.Equal(expected: 1, actual: events.Count(i => i == BeginLoopEventId)); Assert.Equal(expected: 1, actual: events.Count(i => i == BeginInvokeEventId)); const int EndLoopEventId = 2; const int EndInvokeEventId = 4; Assert.Equal(expected: 1, actual: events.Count(i => i == EndLoopEventId)); Assert.Equal(expected: 1, actual: events.Count(i => i == EndInvokeEventId)); const int ForkEventId = 5; const int JoinEventId = 6; Assert.True(events.Count(i => i == ForkEventId) >= 1); Assert.Equal(events.Count(i => i == ForkEventId), events.Count(i => i == JoinEventId)); } }
public void TestEtw() { using (var listener = new TestEventListener(new Guid("159eeeec-4a14-4418-a8fe-faabcd987887"), EventLevel.Verbose)) { var events = new ConcurrentQueue<int>(); listener.RunWithCallback(ev => events.Enqueue(ev.EventId), () => { Enumerable.Range(0, 10000).AsParallel().Select(i => i).ToArray(); }); const int BeginEventId = 1; Assert.Equal(expected: 1, actual: events.Count(i => i == BeginEventId)); const int EndEventId = 2; Assert.Equal(expected: 1, actual: events.Count(i => i == EndEventId)); const int ForkEventId = 3; const int JoinEventId = 4; Assert.True(events.Count(i => i == ForkEventId) > 0); Assert.Equal(events.Count(i => i == ForkEventId), events.Count(i => i == JoinEventId)); } }
public void OutputCountItem(ConcurrentBag<Task> tasks, ConcurrentQueue<int> concurrentQueue) { Console.WriteLine(); foreach (var task in tasks.OrderBy(id => id.Id)) { Console.WriteLine("Поток № {0} - количество элементов в контейнере {1}", task.Id, concurrentQueue.Count(x => x == task.Id)); } Console.WriteLine("\nВсего в контейнере {0} элементов. ", concurrentQueue.Count); Console.ReadKey(); }
static void SemaphoreMultiplex() { counter = 0; Semaphore multiple = new Semaphore(0, 10); multiple.Release(); ConcurrentQueue<int> answer = new ConcurrentQueue<int>(); //multiple. Random ex = new Random(); Thread[] array = new Thread[50]; for (int i = 0; i < 50; i++) { array[i] = new Thread( () => { multiple.WaitOne(); counter = 1 + counter; Thread.Sleep(ex.Next(67)); multiple.Release(); answer.Enqueue(counter); } ); array[i].Start(); } foreach (var t in array) { t.Join(); } var s = answer.Distinct(); foreach (var t in answer) { Console.WriteLine("count {0} and t {1}", t, answer.Count()); }; }
public int FillPageQueue(bool OnlyLarge = false) { PageQueue = new ConcurrentQueue<PFN>(); VIRTUAL_ADDRESS VA; VA.Address = 0; if (DP.PT == null) PageTable.AddProcess(DP, DP.MemAccess); //Parallel.ForEach(DP.TopPageTablePage, (kvp) => foreach (var kvp in DP.TopPageTablePage) { // were at the top level (4th) VA.PML4 = kvp.Key; var pfn = new PFN { PTE = kvp.Value, VA = new VIRTUAL_ADDRESS(VA.PML4 << 39) }; foreach (var DirectoryPointerOffset in DP.PT.ExtractNextLevel(pfn, true, 3)) { if (DirectoryPointerOffset == null) continue; foreach (var DirectoryOffset in DP.PT.ExtractNextLevel(DirectoryPointerOffset, KernelSpace, 2)) { if (DirectoryOffset == null) continue; // if we have a large page we add it now if (DirectoryOffset.PTE.LargePage) PageQueue.Enqueue(DirectoryOffset); // otherwise were scanning lower level entries else if(!OnlyLarge) { foreach (var TableOffset in DP.PT.ExtractNextLevel(DirectoryOffset, KernelSpace, 1)) { if (TableOffset == null) continue; PageQueue.Enqueue(TableOffset); } } } } } //}); return PageQueue.Count(); }
public void pushAllData(IEnumerable<ChannelDataPoints> dataAndChannels) { Logger.LogMessageToDB("Attempting to push data to SensorCloud."); checkInit(); if (dataAndChannels.Count() == 0) { return; } var device = getDevice(); var sensor = getSensor(device); var exceptionQueue = new ConcurrentQueue<Exception>(); Parallel.ForEach(dataAndChannels, dataAndChannel => { try { var channel = getChannel(sensor, dataAndChannel.ChannelID); channel.AddTimeSeriesData(_sampleRate, dataAndChannel.DataPoints); } catch (Exception ex) { exceptionQueue.Enqueue(ex); } }); if (exceptionQueue.Count() > 0) { throw new AggregateException(exceptionQueue); } else { Logger.LogMessageToDB("Data Successfully Pushed To SensorCloud."); } }
public SensorCloudPusher(int deviceId) { this.DPDeviceID = deviceId; using (var dbContext = new DynapowerCloudMonitoringDbContext()) { var DPDevice = dbContext.Devices.Find(this.DPDeviceID); if (DPDevice == null) { throw new Exception("Unknown Dynapower Device"); } this.SensorCloudDeviceID = DPDevice.SensorCloudSettings.DeviceID; this.SensorCloudAPIKey = DPDevice.SensorCloudSettings.APIKey; var device = getDevice(); var sensor = addSensor(device, DPDevice); var exceptions = new ConcurrentQueue<Exception>(); Parallel.ForEach(DPDevice.Registers, reg => { try { var channelID = reg.RegisterNumber.ToString(); var channelDesc = reg.Description; var channelLabel = reg.Alias; addChannel(sensor, channelID, channelLabel, channelDesc); } catch (Exception ex) { exceptions.Enqueue(ex); } }); if (exceptions.Count() > 0) { throw new AggregateException(exceptions); } } _initialized = true; }
public static void InsertData(DataSet dataSet) { if (dataSet == null) throw new ArgumentNullException("DataSet param is null"); try { //ConcurrentDictionary<string, object> cache = new ConcurrentDictionary<string, object>(); ICacheManager cache = new MemoryCacheManager(); var exceptions = new ConcurrentQueue<Exception>(); List<Task> tasks = new List<Task>(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var container = new ModelsContainer()) { foreach (DataTable dt in dataSet.Tables) { //Task task = Task.Factory.StartNew(() => //{ try { Console.WriteLine("----------Table name is : {0}---------", dt.TableName); int cursor = 0; foreach (DataRow dr in dt.Rows) { Record record = new Record(); string brandsName = dr[0].ToString(); var brands = cache.Get<Brands>(brandsName, () => { return new Brands() { Name = brandsName }; }); record.Brands = brands; string modelsName = dr[1].ToString(); var models = cache.Get<Models>(modelsName, () => { return new Models() { Name = modelsName }; }); record.Models = models; record.City = dr[2].ToString(); string dv = dr[3].ToString().Replace(".", ""); string d = string.Format("{0}-{1}-01", dv.Substring(0, 4), dv.Substring(4, 2)).Trim(); var buyYear = cache.Get<BuyYear>(d, () => { return new BuyYear() { Time = Convert.ToDateTime(d) }; }); record.BuyYear = buyYear; d = string.Format("{0}-01-01", dr[4].ToString()); record.Both = DateTime.Parse(d); bool g = dr[5].ToString().Equals("男") ? true : false; record.Gender = Convert.ToBoolean(g); record.Address = dr[6].ToString(); record.Zip = dr[7].ToString(); container.Set<Record>().Add(record); Console.WriteLine("address {0}, cursor = {1}, threadId = {2}", record.Address, cursor, Thread.CurrentThread.ManagedThreadId); cursor++; if (cursor == 100) { cursor = 0; container.SaveChanges(); } } } catch (Exception ex) { exceptions.Enqueue(ex); } //}); //tasks.Add(task); container.SaveChanges(); } } //Task.WaitAll(tasks.ToArray()); stopwatch.Stop(); TimeSpan ts = stopwatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); if (exceptions.Any()) { Console.WriteLine("Parallel have exceptions, count = {0}", exceptions.Count()); } } catch (Exception ex) { string msg = ex.OutputMessage(); Console.WriteLine("{0}", msg); } }
private void BwPreparing_DoWork(object sender, DoWorkEventArgs e) { preparing = true; process = "Preparing"; swPreparing.Start(); DirectoryInfo di = new DirectoryInfo(txtImageFolderPath.Text); Stopwatch diListStopwatch = new Stopwatch(); Stopwatch fileListStopwatch = new Stopwatch(); //Prepare and Setup File Queues for Processing if (di.Exists) { try { //Build Directory Queue diListStopwatch.Start(); IEnumerable<DirectoryInfo> diList = di.EnumerateDirectories("*", SearchOption.AllDirectories).Where( d => d.EnumerateFiles("*.dng", SearchOption.TopDirectoryOnly).Union(d.EnumerateFiles("*.cr2", SearchOption.TopDirectoryOnly)).Count() > 0 && !d.Name.Contains("Dual ISO DNG") && !d.Name.Contains("Dual ISO CR2") && !d.Name.Contains("Dual ISO Original CR2") && !d.Name.Contains("logs")); dirQueue = new ConcurrentQueue<DirectoryInfo>(diList); //How many directories in play? maxDirQueueCount = dirQueue.Count; diListStopwatch.Stop(); //How long did it take to get a directory listing and build dir queue? string diListDuration = diListStopwatch.Elapsed.ToString(); //Build File Queue fileListStopwatch.Start(); List<FileInfo> fileList = new List<FileInfo>(); //Search root level for files fileList.AddRange(di.EnumerateFiles("*.dng", SearchOption.TopDirectoryOnly).Union(di.EnumerateFiles("*.cr2", SearchOption.TopDirectoryOnly)).ToList()); //Search subdirectories for files foreach (DirectoryInfo dInfo in diList) { string path = dInfo.FullName; fileList.AddRange(dInfo.EnumerateFiles("*.dng", SearchOption.TopDirectoryOnly).Union(dInfo.EnumerateFiles("*.cr2", SearchOption.TopDirectoryOnly)).ToList()); DirectoryInfo dInfoDequeue; dirQueue.TryDequeue(out dInfoDequeue); } fileQueue = new ConcurrentQueue<FileInfo>(fileList); //How many files in play? maxFileQueueCount = fileQueue.Count; //Get file count for first folder being processed if (maxFileQueueCount > 0) { FileInfo subfolderFileInfo; fileQueue.TryPeek(out subfolderFileInfo); if (subfolderFileInfo != null) { subFolderFileQueue = new ConcurrentQueue<FileInfo>( subfolderFileInfo.Directory.EnumerateFiles("*.dng", SearchOption.TopDirectoryOnly).Union( subfolderFileInfo.Directory.EnumerateFiles("*.cr2", SearchOption.TopDirectoryOnly)).ToList() ); maxSubfolderFileQueueCount = subFolderFileQueue.Count(); } } fileListStopwatch.Stop(); //How long did it take to get all file listings and build file queue? string fileListDuration = fileListStopwatch.Elapsed.ToString(); btnBusiness_Click(this, new EventArgs()); } catch (Exception ex) { if (di != null) { File.WriteAllText(di.FullName + "\\ExceptionLog." + Guid.NewGuid().ToString() + ".log", ex.Message + "\r\n" + ex.StackTrace); } } } else { BeginInvoke((MethodInvoker)delegate { FindAndMoveMsgBox("Path Error", this); MessageBox.Show(this, "Image Folder Path Invalid", "Path Error"); }); } e.Cancel = true; }
private void BwBusiness_DoWork(object sender, DoWorkEventArgs e) { Stopwatch directoryStopwatch = new Stopwatch(); threadCount++; do { //Get available memory var computerInfo = new ComputerInfo(); var availablePhysicalMemory = (decimal)(computerInfo.AvailablePhysicalMemory); var totalPhysicalMemory = (decimal)(computerInfo.TotalPhysicalMemory); var freeMemRatio = availablePhysicalMemory / totalPhysicalMemory; //Memory protection check if (freeMemRatio > (decimal).09) { bool errorFlag = false; if (!directoryStopwatch.IsRunning) { directoryStopwatch.Start(); } //Get file info so that any necessary logging can be written to the //appropriate folder FileInfo fiPeek; fileQueue.TryPeek(out fiPeek); try { Stopwatch swFile = new Stopwatch(); swFile.Start(); bool isCR2Processing = false; //Keep trying to get a file from the queue FileInfo fi; while (!fileQueue.TryDequeue(out fi) && fileQueue.Count() != 0) { } if (fi != null && File.Exists(fi.FullName)) { string filePath = fi.FullName; string directoryPath = fi.DirectoryName; currentDirectory = directoryPath; //Create Subfolders for Processed DNGs and Logs if (fi.Name.ToLower().Contains(".dng")) { fi.Directory.CreateSubdirectory("Dual ISO DNG"); } if (fi.Name.ToLower().Contains(".cr2")) { isCR2Processing = true; fi.Directory.CreateSubdirectory("Dual ISO CR2"); fi.Directory.CreateSubdirectory("Dual ISO Original CR2"); } fi.Directory.CreateSubdirectory("logs"); //Handle condition where application exited before files moved if (File.Exists(fi.FullName.ToLower().Replace(".dng", ".cr2"))) { isCR2Processing = true; } //Get initial image path pre-move string initialImagePath = fi.FullName; //Get initial log path pre-move string initialLogPath = initialImagePath + ".log"; //Get log directory path string logDirectoryPath = fi.Directory.EnumerateDirectories().Where( d => d.Name.Contains("logs")).FirstOrDefault().FullName; //Get destination log path string destLogPath = fi.Directory.EnumerateDirectories().Where( d => d.Name.Contains("logs")).FirstOrDefault().FullName + "\\" + fi.Name + ".log"; //Get destination image path string destImagePath = string.Empty; if (fi.Name.ToLower().Contains(".dng")) { if (!isCR2Processing) { destImagePath = fi.Directory.EnumerateDirectories().Where( d => d.Name.Contains("Dual ISO DNG")).FirstOrDefault().FullName + "\\" + fi.Name; } else { destImagePath = fi.Directory.EnumerateDirectories().Where( d => d.Name.Contains("Dual ISO CR2")).FirstOrDefault().FullName + "\\" + fi.Name; } } else if (fi.Name.ToLower().Contains(".cr2")) { destImagePath = fi.Directory.EnumerateDirectories().Where( d => d.Name.Contains("Dual ISO Original CR2")).FirstOrDefault().FullName + "\\" + fi.Name; } string initialDNGPath = string.Empty; string destDNGPath = string.Empty; if (isCR2Processing) { initialDNGPath = initialImagePath.ToLower().Replace(".cr2", ".dng").ToUpper(); destDNGPath = destImagePath.Replace("Dual ISO Original CR2", "Dual ISO CR2").ToLower().Replace(".cr2", ".dng").ToUpper(); } string output = string.Empty; string errorOutput = string.Empty; //Get available memory availablePhysicalMemory = (decimal)(computerInfo.AvailablePhysicalMemory); totalPhysicalMemory = (decimal)(computerInfo.TotalPhysicalMemory); freeMemRatio = availablePhysicalMemory / totalPhysicalMemory; //Memory protection check if (!formClosed && freeMemRatio > (decimal).09) { // Use ProcessStartInfo class. ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.FileName = "\"" + txtCr2hdrPath.Text + "\""; startInfo.WorkingDirectory = txtCr2hdrPath.Text.Length > 0 ? txtCr2hdrPath.Text.Substring(0, txtCr2hdrPath.Text.IndexOf(".exe")).Substring(0, txtCr2hdrPath.Text.LastIndexOf("\\")) : startInfo.WorkingDirectory; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.Arguments = "\"" + filePath + "\""; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; Process exeProcess = Process.Start(startInfo); // Start the process with the info we specified. // Call WaitForExit and then the using-statement will close. using (exeProcess) { output = exeProcess.StandardOutput.ReadToEnd(); errorOutput = exeProcess.StandardError.ReadToEnd(); exeProcess.WaitForExit(); output += "\r\nProcess Errored?: "; //Check exitcode to see if process errored or not if (exeProcess.ExitCode == 0) { output += "No"; } else { output += "Yes"; errorCount++; errorFlag = true; } output += "\r\n" + errorOutput; output += "\r\n\r\n" + exeProcess.TotalProcessorTime.ToString() + " process duration."; string processDuration = exeProcess.TotalProcessorTime.ToString(); } //Move Image if Processing was Successful if (!errorFlag) { if (File.Exists(initialImagePath)) { if (!File.Exists(destImagePath)) { fi.MoveTo(destImagePath); } else { if (destImagePath.ToLower().Contains(".dng")) { fi.MoveTo(destImagePath.ToLower().Replace(".dng", "." + Guid.NewGuid().ToString() + ".dng")); } else if (destImagePath.ToLower().Contains(".cr2")) { fi.MoveTo(destImagePath.ToLower().Replace(".cr2", "." + Guid.NewGuid().ToString() + ".cr2")); } } imagesProcessed++; FileInfo subfolderFileInfoDequeued; subFolderFileQueue.TryDequeue(out subfolderFileInfoDequeued); } } swFile.Stop(); output += "\r\n" + swFile.Elapsed.ToString() + " file activity duration."; //Write appropriate flavor of output log if (errorFlag) { File.WriteAllText(filePath + ".ProcessErrored.log", output); } else { File.WriteAllText(filePath + ".log", output); } //Move Log and perform cleanup if Processing was Successful if (!errorFlag) { if (File.Exists(initialLogPath)) { if (!File.Exists(destLogPath)) { File.Move(initialLogPath, destLogPath); } else { File.Move(initialLogPath, destLogPath.Replace(".log", "." + Guid.NewGuid().ToString() + ".log")); } } if (isCR2Processing) { //Move resultant DNG if CR2 was processed if (File.Exists(initialDNGPath)) { if (!File.Exists(destDNGPath)) { File.Move(initialDNGPath, destDNGPath); } else { File.Move(initialDNGPath, (destDNGPath.ToLower().Replace(".dng", "." + Guid.NewGuid().ToString() + ".dng"))); } } else //Resultant DNG leftover while CR2 still in folder { DirectoryInfo dirInfo = new DirectoryInfo(currentDirectory); dirInfo.CreateSubdirectory("Dual ISO Original CR2"); string leftoverInitialCR2Path = initialImagePath.ToLower().Replace(".dng", ".cr2").ToUpper(); string leftoverDestCR2Path = destImagePath.Replace( "Dual ISO CR2", "Dual ISO Original CR2").ToLower().Replace(".dng", ".cr2").ToUpper(); if (File.Exists(leftoverInitialCR2Path)) { if (!File.Exists(leftoverDestCR2Path)) { File.Move(leftoverInitialCR2Path, leftoverDestCR2Path); } else { File.Move(leftoverInitialCR2Path, leftoverDestCR2Path.ToLower().Replace(".cr2", "." + Guid.NewGuid().ToString() + ".cr2")); } } } } string tempDurationPerImage = durationPerImage.ToString(); //Anything else left in the queue? if (fileQueue.Count() > 0) { FileInfo fiPeekNext; while (!fileQueue.TryPeek(out fiPeekNext) && fileQueue.Count() != 0) { } //Switching to a new folder? Clean up logs after making sure there are no images left in folder. if ( (fiPeekNext.DirectoryName != directoryPath && Directory.EnumerateFiles( directoryPath, "*.dng", SearchOption.TopDirectoryOnly).Union(Directory.EnumerateFiles( directoryPath, "*.cr2", SearchOption.TopDirectoryOnly)).Count() == 0) || fiPeekNext == null) { directoryStopwatch.Stop(); subFolderFileQueue = new ConcurrentQueue<FileInfo>( fiPeekNext.Directory.EnumerateFiles("*.dng", SearchOption.TopDirectoryOnly).Union( fiPeekNext.Directory.EnumerateFiles("*.cr2", SearchOption.TopDirectoryOnly)).ToList() ); maxSubfolderFileQueueCount = subFolderFileQueue.Count(); string directoryDuration = directoryStopwatch.Elapsed.ToString() + " Directory Duration."; directoryDuration += "\r\n" + tempDurationPerImage + " Duration Per Image."; if (!File.Exists(directoryPath + "\\directoryDuration.log")) { File.WriteAllText(directoryPath + "\\directoryDuration.log", directoryDuration); } directoryStopwatch.Reset(); IEnumerable<FileInfo> logs = new DirectoryInfo(directoryPath).EnumerateFiles("*.log", SearchOption.TopDirectoryOnly); if (logs.Count() > 0) { foreach (FileInfo log in logs) { if (Directory.Exists(log.DirectoryName + "\\logs")) { log.MoveTo(log.DirectoryName + "\\logs\\" + log.Name); } } } } } else { //No more files in queue. Clean up logs for the final time after making sure there are no image files remaining if (Directory.EnumerateFiles(directoryPath, "*.dng", SearchOption.TopDirectoryOnly).Union( Directory.EnumerateFiles(directoryPath, "*.cr2", SearchOption.TopDirectoryOnly)).Count() == 0) { directoryStopwatch.Stop(); string directoryDuration = directoryStopwatch.Elapsed.ToString() + " Directory Duration."; directoryDuration += "\r\n" + tempDurationPerImage + " Duration Per Image."; if (!File.Exists(directoryPath + "\\directoryDuration.log")) { File.WriteAllText(directoryPath + "\\directoryDuration.log", directoryDuration); } directoryStopwatch.Reset(); IEnumerable<FileInfo> logs = new DirectoryInfo(directoryPath).EnumerateFiles("*.log", SearchOption.TopDirectoryOnly); if (logs.Count() > 0) { foreach (FileInfo log in logs) { if (Directory.Exists(log.DirectoryName + "\\logs")) { if (File.Exists(log.FullName)) { log.MoveTo(log.DirectoryName + "\\logs\\" + log.Name); } } } } } } } } else { //Memory protection check failed? Put file back into queue fileQueue.Enqueue(fi); } } if (isCR2Processing) { //If processing CR2, sleep thread for between 5 and 10 seconds Random r = new Random(); Thread.Sleep(r.Next(5000, 10000)); } } catch (Exception ex) { if (fiPeek != null) { File.WriteAllText(fiPeek.DirectoryName + "\\ExceptionLog." + fiPeek.Name + "." + Guid.NewGuid().ToString() + ".log", ex.Message + "\r\n" + ex.StackTrace); } } } } while (fileQueue.Count() > 0 && !((BackgroundWorker)(sender)).CancellationPending && !formClosed); //Queue empty and last worker? Time to clean up logs and signal work completed. if (fileQueue.Count() == 0 && threadCount == 1) { workCompleted = true; if (!bwCancel.IsBusy) { bwCancel.RunWorkerAsync(); Cancel(); //Clean up logs DirectoryInfo di = new DirectoryInfo(txtImageFolderPath.Text); if (di.Exists) { try { IEnumerable<DirectoryInfo> diList = di.EnumerateDirectories("*", SearchOption.AllDirectories).Where( d => d.EnumerateFiles("*.log", SearchOption.TopDirectoryOnly).Count() > 0 && !d.Name.Contains("Dual ISO DNG") && !d.Name.Contains("Dual ISO CR2") && !d.Name.Contains("Dual ISO Original CR2") && !d.Name.Contains("logs")); List<FileInfo> logList = new List<FileInfo>(); //We don't want to clean up the process errored logs, //since we want to keep those next to the files that need to be reprocessed. logList.AddRange(di.EnumerateFiles("*.log", SearchOption.TopDirectoryOnly).Where( lgFile => !lgFile.Name.Contains("ProcessErrored")).ToList()); foreach (DirectoryInfo dInfo in diList) { string path = dInfo.FullName; logList.AddRange(dInfo.EnumerateFiles("*.log", SearchOption.TopDirectoryOnly).Where( lgFile => !lgFile.Name.Contains("ProcessErrored")).ToList()); } foreach (FileInfo log in logList) { if (Directory.Exists(log.DirectoryName + "\\logs")) { log.MoveTo(log.DirectoryName + "\\logs\\" + log.Name); } } } catch (Exception ex) { File.WriteAllText(di.FullName + "\\ExceptionLog.WorkComplete." + Guid.NewGuid().ToString() + ".log", ex.Message + "\r\n" + ex.StackTrace); } } } } threadCount--; }