private void HandleTopologyChange(DatabaseRecord newRecord) { var instancesToDispose = new List <OutgoingReplicationHandler>(); if (newRecord == null || _server.IsPassive()) { DropOutgoingConnections(Destinations, instancesToDispose); _internalDestinations.Clear(); _externalDestinations.Clear(); _destinations.Clear(); DisposeConnections(instancesToDispose); return; } _clusterTopology = GetClusterTopology(); HandleInternalReplication(newRecord, instancesToDispose); HandleExternalReplication(newRecord, instancesToDispose); var destinations = new List <ReplicationNode>(); destinations.AddRange(_internalDestinations); destinations.AddRange(_externalDestinations); _destinations = destinations; _numberOfSiblings = _destinations.Select(x => x.Url).Intersect(_clusterTopology.AllNodes.Select(x => x.Value)).Count(); DisposeConnections(instancesToDispose); }
static void Main(string[] args) { Console.WriteLine("--- Testing Parallelism ---"); timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed); timer.Enabled = true; while (timer.Enabled) { Task.Run(() => DateTimes.Add(DateTime.UtcNow.AddHours(Timezone))); } int Count = DateTimes.Count; Console.WriteLine($"Asyncronous: {DateTimes.Count}"); timer.Close(); timer.Dispose(); timer = new Timer(Time); timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed); DateTimes.Clear(); timer.Enabled = true; while (timer.Enabled) { DateTimes.Add(DateTime.UtcNow.AddHours(Timezone)); } Console.WriteLine($"Syncronous: {DateTimes.Count}\r\n{DateTimes.Count / Count}x Faster"); DateTimes.Clear(); timer.Close(); timer.Dispose(); Console.WriteLine("Press any key to quit."); Console.ReadKey(); }
public async Task <TaskResult <ResourceCompleterCheckResult?> > CheckAndDownloadTaskAsync() { _retryFiles.Clear(); if (!(ResourceInfoResolvers?.Any() ?? false)) { return(new TaskResult <ResourceCompleterCheckResult?>(TaskResultStatus.Success, value: null)); } var totalLostFiles = new List <IGameResource>(); foreach (var resolver in ResourceInfoResolvers) { resolver.GameResourceInfoResolveEvent += GameResourceInfoResolveStatus; await foreach (var lostFile in resolver.ResolveResourceAsync()) { totalLostFiles.Add(lostFile); } } if (!totalLostFiles.Any()) { return(new TaskResult <ResourceCompleterCheckResult?>(TaskResultStatus.Success, value: null)); } totalLostFiles.Shuffle(); NeedToDownload = totalLostFiles.Count; var downloadList = ( from f in totalLostFiles select new DownloadFile { Completed = WhenCompleted, DownloadPath = f.Path, DownloadUri = f.Uri, FileName = f.FileName, FileSize = f.FileSize, CheckSum = f.CheckSum, FileType = f.Type, TimeOut = 10000 }).ToList(); if (downloadList.First().FileType.Equals("GameJar", StringComparison.OrdinalIgnoreCase)) { downloadList.First().Changed = (_, args) => { DownloadFileCompletedEvent?.Invoke(this, new DownloadFileCompletedEventArgs(null, null, downloadList.First(), args.Speed)); }; } var(item1, item2) = await DownloadFiles(downloadList); return(new TaskResult <ResourceCompleterCheckResult?>(item1, value: item2)); }
public Request[] GetAllTimeoutList() { var data = _queue.ToArray(); #if NETSTANDARD2_0 Interlocked.Exchange(ref _queue, new ConcurrentBag <Request>()); #else _queue.Clear(); #endif return(data); }
public async Task CredentialProofDemo() { var events = 0; _eventAggregator.GetEventByType <ServiceMessageProcessingEvent>() .Where(_ => (_.MessageType == MessageTypes.PresentProofNames.RequestPresentation || _.MessageType == MessageTypes.PresentProofNames.Presentation)) .Subscribe(_ => { events++; }); //Setup a connection and issue the credentials to the holder var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet); await Scenarios.IssueCredentialAsync( _schemaService, _credentialService, _messages, issuerConnection, holderConnection, _issuerWallet, _holderWallet, await _holderWallet.Pool, TestConstants.DefaultMasterSecret, true, new List <CredentialPreviewAttribute> { new CredentialPreviewAttribute("first_name", "Test"), new CredentialPreviewAttribute("last_name", "Holder") }); _messages.Clear(); //Requestor initialize a connection with the holder var(holderRequestorConnection, requestorConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _holderWallet, _requestorWallet); await Scenarios.ProofProtocolAsync(_proofService, _messages, holderRequestorConnection, requestorConnection, _holderWallet, _requestorWallet, new ProofRequest() { Name = "ProofReq", Version = "1.0", Nonce = await AnonCreds.GenerateNonceAsync(), RequestedAttributes = new Dictionary <string, ProofAttributeInfo> { { "first-name-requirement", new ProofAttributeInfo { Name = "first_name" } } } }); _messages.Clear(); Assert.True(events == 2); }
public static void Clear_ConcurrentUsage_NoExceptions(int threadsCount, int itemsPerThread) { var bag = new ConcurrentBag <int>(); Task.WaitAll((from i in Enumerable.Range(0, threadsCount) select Task.Run(() => { var random = new Random(); for (int j = 0; j < itemsPerThread; j++) { int item; switch (random.Next(5)) { case 0: bag.Add(j); break; case 1: bag.TryPeek(out item); break; case 2: bag.TryTake(out item); break; case 3: bag.Clear(); break; case 4: bag.ToArray(); break; } } })).ToArray()); }
protected void RestartFiltering(string searchTerm) { lock (_taskCancellationLock) { //cancel any previous searches foreach (var c in _cancelFiltering) { c.Cancel(); } _cancelFiltering.Clear(); } var cts = new CancellationTokenSource(); _cancelFiltering.Add(cts); _currentFilterTask = Task.Run(() => { var result = BuildList(GetListAfterSearch(searchTerm, cts.Token)); lock (_taskCancellationLock) { _collection = result; _changes = true; } }); }
public void ClearData() { collectedData.Clear(); tripDatas.Clear(); stopDatas.Clear(); weatherDatas.Clear(); }
public static void Clear_AddItemsToThisAndOtherThreads_EmptyAfterClear(bool addToLocalThread, int otherThreads) { var bag = new ConcurrentBag <int>(); const int ItemsPerThread = 100; for (int repeat = 0; repeat < 2; repeat++) { // If desired, add items on other threads if (addToLocalThread) { for (int i = 0; i < ItemsPerThread; i++) { bag.Add(i); } } // If desired, add items on other threads int origThreadId = Environment.CurrentManagedThreadId; Task.WaitAll((from _ in Enumerable.Range(0, otherThreads) select Task.Factory.StartNew(() => { Assert.NotEqual(origThreadId, Environment.CurrentManagedThreadId); for (int i = 0; i < ItemsPerThread; i++) { bag.Add(i); } }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)).ToArray()); // Make sure we got the expected number of items, then clear, and make sure it's empty Assert.Equal((ItemsPerThread * otherThreads) + (addToLocalThread ? ItemsPerThread : 0), bag.Count); bag.Clear(); Assert.Equal(0, bag.Count); } }
public string RunStep(RuntimeSettings settings, ILog log) { var links = new ConcurrentBag <string>(); var results = new StringBuilder(); foreach (string?chapter in settings.TocContents.Chapters) { log.Info("Processing chapter: {0}", chapter); results.AppendFormat("## {0}\r\n\r\n", chapter); links.Clear(); Parallel.ForEach(settings.TocContents.GetLinksForChapter(chapter), link => { var input = settings.SourceDirectory.Combine(link.Url); var contents = input.ReadFile(log); foreach (Match?match in _link.Matches(contents)) { if (match != null) { links.Add(match.Value); } } }); foreach (string link in links.Distinct().OrderBy(s => s)) { results.AppendLine(link); } results.AppendLine(); } return(results.ToString()); }
public override async Task <WsMessage> Invoke() { var allConfirmTransactions = _confirmedTransactions.Select(tx => tx.Tx.Hash).ToList(); if (allConfirmTransactions.IsEmpty()) { return(null); } var model = new ConfirmStateModel { Confirms = allConfirmTransactions, }; if (Program.Starter.CurrentWallet != null) { var accounts = Program.Starter.CurrentWallet.GetAccounts().Select(a => a.ScriptHash).ToList(); model.MyConfirms = _confirmedTransactions.Where(tx => tx.Transfers.Any(t => accounts.Contains(t.From) || accounts.Contains(t.To))).Select(tx => tx.Tx.Hash).ToList(); } _confirmedTransactions.Clear(); return(new WsMessage() { MsgType = WsMessageType.Push, Method = "getLastConfirmTransactions", Result = model, }); }
public static void Clear_ConcurrentUsage_NoExceptions(int threadsCount, int itemsPerThread) { var bag = new ConcurrentBag <int>(); Task.WaitAll((from i in Enumerable.Range(0, threadsCount) select Task.Factory.StartNew(() => { var random = new Random(); for (int j = 0; j < itemsPerThread; j++) { int item; switch (random.Next(5)) { case 0: bag.Add(j); break; case 1: bag.TryPeek(out item); break; case 2: bag.TryTake(out item); break; case 3: bag.Clear(); break; case 4: bag.ToArray(); break; } } }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)).ToArray()); }
public static void ExecuteAction() { if (!_taskCollection.Any() || !_taskCompleted) { return; } _taskCompleted = false; var failedActions = new ConcurrentBag <Action>(); foreach (var action in _taskCollection) { try { action.DynamicInvoke(); } catch (Exception) { failedActions.Add(action); } } _taskCollection.Clear(); _taskCollection = failedActions; _taskCompleted = true; }
private static async Task SendMessage() { var message = string.Join(Environment.NewLine, Package.ToArray()); await APIHelper.DiscordAPI.SendMessageAsync(SettingsManager.Settings.SystemLogFeederModule.DiscordChannelId, message); Package.Clear(); }
private void TimerActivation() { switch (_queueServiceParams.OnErrorAction) { case QueueConfiguration.ErrorAction.NackOnException: { foreach (var message in _toBeNackedMessages) { if (Channel != null && !Channel.IsClosed) { Channel.BasicNack(message, false, true); } } _toBeNackedMessages.Clear(); return; } case QueueConfiguration.ErrorAction.RestartConnection: default: { Start(); return; } } }
private void Calculate() { var lo = ulong.Parse(fromBox.Text); var hi = ulong.Parse(toBox.Text); if (lo != _lo || hi != _hi) // Don't redraw if nothing changed { _lo = lo; _hi = hi; CollatzCalculator calculator = new CollatzCalculator(); _coords.Clear(); // Calculate for each seed and save result Parallel.For((long)_lo, (long)_hi, seed => { var entry = calculator.GetEntry((ulong)seed); _coords.Add(entry); }); _maxCount = _coords.Max(v => v.Count); _maxPeak = _coords.Max(v => v.Peak); } }
private static void ReConnect() { if (Socket != null) { Socket.Close(); } try { Socket = new Socket(SocketType.Stream, ProtocolType.Tcp); Socket.Connect(IPAddress.Parse("127.0.0.1"), 23333); var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(PackStart) + " "); data[data.Length - 1] = 0; Socket.Send(data); QueueSend.Clear(); Logs.LogWrite("机器人已连接"); IsConnect = true; } catch (Exception e) { Logs.LogError("机器人连接失败"); Logs.LogError(e); } }
public async Task FreeMemory() { for (int i = 0; i < _parallelCompilations; i++) { await _semaphore.WaitAsync(); await _semaphoreCss.WaitAsync(); } try { foreach (var compiler in _pool) { compiler.Dispose(); } _pool.Clear(); foreach (var processor in _poolCss) { processor.Dispose(); } _poolCss.Clear(); } finally { for (int i = 0; i < _parallelCompilations; i++) { _semaphore.Release(); _semaphoreCss.Release(); } } }
public override void Dispose() { base.Dispose(); _reader?.Dispose(); _collection.Clear(); }
public async Task InitStaticData() { var stops = await _stopClient.GetAllStops(); if (StopTypeCache == null) { StopTypeCache = new ConcurrentDictionary <int, StopType>(); } if (Stops == null) { Stops = new ConcurrentBag <StopBase>(); } if (SimplifiedNames == null) { SimplifiedNames = new ConcurrentDictionary <int, string>(); } StopTypeCache.Clear(); Stops.Clear(); SimplifiedNames.Clear(); foreach (var stop in stops) { StopTypeCache.AddOrUpdate(stop.Id, stop.Type, (_, __) => stop.Type); Stops.Add(new StopBase { Id = stop.Id, Name = stop.Name.Trim() }); SimplifiedNames.AddOrUpdate(stop.Id, stop.Name.PrepareForCompare(), (_, __) => stop.Name.PrepareForCompare()); } }
private void OpenOrdersTask(string exchange) { var api = m_api[exchange]; int count = 0; while (true) { var openOrders = api.GetOpenOrderDetails(); openOrders = openOrders.OrderBy(o => o.Symbol); m_orders.Clear(); int orderCount = 0; ++count; if (count % 3 == 0) { Console.WriteLine("\n{0}", DateTime.Now); } foreach (var oo in openOrders) { m_orders.Add(oo); if (count % 3 == 0) { Console.WriteLine(">>> {0,2}) {1}", ++orderCount, oo.ToStr()); } } Thread.Sleep(m_testOnly ? 15000 : 5000); } }
public SlangdefineTitleProvider(ILogger <SlangdefineTitleProvider> logger) { _logger = logger; Task.Run(async() => { while (!_cacheRenewCts.IsCancellationRequested) { _cacheRenewMre.Wait(_cacheRenewCts.Token); try { if (_cacheRenewCts.IsCancellationRequested) { return; } var data = await _cacheRenewPolicy.ExecuteAsync(async ctx => await FetchAsync(Parse, ctx), _cacheRenewCts.Token); if (data.Any()) { _cache.Clear(); data.ToList().ForEach(d => _cache.Add(d)); _lastRenewUtc = DateTime.UtcNow; } } catch (Exception e) { _logger.LogError(e, "Cache renew loop"); } finally { _cacheRenewMre.Reset(); } } }, _cacheRenewCts.Token); }
public void BulkHeadRejectsExecutionWhenQueueFull() { _processed.Clear(); _rejected.Clear(); bool exceptionThrown = false; var bulkHead = Policy.Bulkhead(1, 5, context => { var id = context["id"]; Console.Out.WriteLine($"Rejected id {id}"); _rejected.Add((int)id); }); // max concurrency of 2, max queue size of 5 Parallel.ForEach(_testData, id => { try { var context = new Polly.Context { ["id"] = id }; bulkHead.Execute((ctx) => SlowFunction(id), context); } catch (BulkheadRejectedException) { // keep demo running exceptionThrown = true; } }); Assert.IsTrue(exceptionThrown); Assert.IsTrue(_processed.Count > 0); Assert.IsTrue(_rejected.Count > 0); Assert.IsTrue(_rejected.Count > _processed.Count); // we will always reject more since method takes 1 second }
public static bool Remove <T>(this ConcurrentBag <T> t, T item) { bool isFind = false; List <T> temp = new List <T>(); lock (t) { foreach (var i in t) { if (!i.Equals(item)) { temp.Add(i); isFind = true; } } if (isFind) { t.Clear(); foreach (var i in temp) { t.Add(i); } } } return(isFind); }
protected virtual void Dispose(bool disposing) { if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 1) { return; } if (disposing) { //Log.Debug("Disposed object pool of {ItemType} (Created={Created}; Rented={Rented}; Returned={Returned}).", typeof(TItem), _created, _rented, _returned); //if (_rented > _returned) //{ // Log.Warn("Several {ItemType} items ({Count}) were not returned.", typeof(TItem), _rented - _returned); //} if (_isItemDisposable) { foreach (IDisposable item in _objects) { item.Dispose(); } } _objects.Clear(); } }
/// <summary> /// #TODO: Document /// </summary> internal virtual void Solve() { initialState.Clear(); solutions.Clear(); ThreadPool.QueueUserWorkItem(SolveCallback, initialState); terminateEvent.WaitOne(timeout); }
public void Stop() { _logger.LogInformation("otf stop request."); TODO.CollectionChanged -= Source_CollectionChanged; TODO.Clear(); fsBag.Clear(); foreach (string path in WATCHER.Keys) { WATCHER[path].EnableRaisingEvents = false; WATCHER[path].Dispose(); WATCHER[path] = null; } WATCHER.Clear(); WATCHER = null; foreach (Task task in TASKS.Keys) { try { TASKS[task].Cancel(); //task.Wait(); TASKS[task].Dispose(); } catch (AggregateException ex) { _logger.LogError("Task cancel failed :( {0}", ex.InnerExceptions[0].Message); } } TASKS.Clear(); TASKS = null; Running = false; _logger.LogInformation("otf stop."); }
public static void TestPhaseNaiveParallel() { var result = new ConcurrentBag <Price>(); var partitioner = Partitioner.Create(0, TestData.Count); Parallel.ForEach(partitioner, range => { var subList = TestData.Skip(range.Item1).Take(range.Item2 - range.Item1).ToList(); using (var context = CreateContext()) { foreach (var testElement in subList) { var query = context.Prices.Where( x => x.Security.Ticker == testElement.Ticker && x.TradedOn == testElement.TradedOn && x.PriceSourceId == testElement.PriceSourceId); foreach (var el in query) { result.Add(el); } } } }); result.Clear(); }
public async Task CanEstablishConnectionsWithMultiPartyInvitationAsync() { (var invite, var record) = await _connectionService.CreateInvitationAsync(_issuerWallet, new InviteConfiguration { MultiPartyInvitation = true }); var(connectionIssuer, connectionHolderOne) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet, invite, record.Id); _messages.Clear(); var(connectionIssuerTwo, connectionHolderTwo) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWalletTwo, invite, record.Id); Assert.Equal(ConnectionState.Connected, connectionIssuer.State); Assert.Equal(ConnectionState.Connected, connectionHolderOne.State); Assert.Equal(ConnectionState.Connected, connectionIssuerTwo.State); Assert.Equal(ConnectionState.Connected, connectionHolderTwo.State); Assert.Equal(connectionIssuer.MyDid, connectionHolderOne.TheirDid); Assert.Equal(connectionIssuer.TheirDid, connectionHolderOne.MyDid); Assert.Equal(connectionIssuerTwo.MyDid, connectionHolderTwo.TheirDid); Assert.Equal(connectionIssuerTwo.TheirDid, connectionHolderTwo.MyDid); Assert.Equal(connectionIssuer.Endpoint.Uri, TestConstants.DefaultMockUri); Assert.Equal(connectionIssuerTwo.Endpoint.Uri, TestConstants.DefaultMockUri); }
public void Execute() { var list = new ConcurrentBag <string>(); string[] directoryNames = { @"D:\Ashok", @"D:\Office" }; var timer = Stopwatch.StartNew(); timer.Start(); foreach (var directory in directoryNames) { string[] paths = Directory.GetFiles(directory); foreach (var path in paths) { list.Add(path); } } list.Clear(); timer.Stop(); Console.WriteLine($"Took { timer.ElapsedMilliseconds } milliseconds time to read all files from both the directories"); foreach (var item in list) { Console.WriteLine(item); } timer.Start(); Parallel.ForEach(directoryNames, (currentDirectory) => { string[] paths = Directory.GetFiles(currentDirectory); foreach (var path in paths) { list.Add(path); } }); timer.Stop(); Console.WriteLine($"Parallel.ForEach took { timer.ElapsedMilliseconds } milliseconds time to read all files from both the directories"); }