• About
  • Privacy Policy
  • Utilities
Darchuk.NET

Concurrency

Waiting for a keypress asynchronously in a C# console app

February 8, 2019 by Robert 2 Comments

This one’s kind of specific, but sometimes when you’re writing a console app that does a lot of asynchronous stuff, and you want to cancel it at any time by pressing escape.  via GIPHY However, Console.ReadKey is blocking… so that’s not super great. CancellationToken First, make use of the cancellation tokens.  I’ve talked about them … [Read more…]

Posted in: .NET Core, Asynchronous, C#, Concurrency Tagged: async, C#

CancellationTokens

November 2, 2018 by Robert 1 Comment

In C# asynchronous programming, a CancellationToken allows you to stop a Task.  This is especially useful if you have many Tasks running and want to gracefully shut down the program, or if you want to add a timeout to a Task. via GIPHY Creation CancellationTokens are created from a CancellationTokenSource. via GIPHY Usage The token … [Read more…]

Posted in: .NET Core, Asynchronous, C#, Concurrency Tagged: async, await, C#, CancellationToken, CancellationTokenSource, Concurrency, OperationCanceledException, TaskCanceledException

Jasmine–Testing code that contains setTimeout

September 14, 2018 by Robert Leave a Comment

Recently I wrote some code that contained a setTimeout.  The reason was so that the UI could catch up before the function actually fired – something that I’ve found the need to do with javascript from time to time. So, when it comes to testing the code… my test just skipped along until it literally … [Read more…]

Posted in: Angular, Asynchronous, Concurrency, Jasmine, JavaScript, Typescript, Unit Testing, Web Development Tagged: Angular, async, Jasmine, setTimeout, Testing, Typescript, unit testing

Jasmine–Async callback was not invoked within timeout

August 10, 2018 by Robert Leave a Comment

Recently I was updating and fixing some existing unit tests after doing a (rather large) overhaul of code.The unit test previously needed to wait for an asynchronous callback to finish before continuing, something like this: Note the optional done parameter provided to beforeEach.  This is a void function that signals Jasmine that the callback is … [Read more…]

Posted in: Angular, Asynchronous, Concurrency, Jasmine, JavaScript, Typescript, Web Development Tagged: async, Asynchronous, beforeEach, Callback, Jasmine, specified, test, Testing, timeout, unit testing

Throttling execution through BlockingCollection

July 20, 2018 by Robert Leave a Comment

BlockingCollection is essentially a queue that provides a thread-safe way to consume items.  You can put it into a loop and consume the items one at a time, throttling execution and potentially preventing your system from being overwhelmed. I use these all the time, but most recently I built it in to a metric collection … [Read more…]

Posted in: C#, Concurrency Tagged: Blocking, BlockingCollection, C#, Concurrency, Execution, Optimization, Process, Queue, Throttle

Thread-Safe Atomic Operations

June 29, 2018 by Robert Leave a Comment

There’s been more than a couple times where I’ve wanted to use the Parallel class to take a collection and rip through it with multiple threads, but I’ve struggled with how to log the progress (especially the count of objects processed) easily. Interlocked .NET actually provides the solution in a static class called Interlocked, located … [Read more…]

Posted in: .NET Core, ASP.NET, Asynchronous, C#, Concurrency Tagged: .NET, C#, Concurrency, Thread, Thread Safe, threading

TypeScript async / await

May 4, 2018 by Robert Leave a Comment

JavaScript is pretty dumb.  I know its the most popular language right now (due to the pervasiveness of browsers and other things that can run it), but, I mean, its just… dumb. Luckily TypeScript is pretty awesome.  It takes a lot of what makes JavaScript stupid and fixes it.  It makes it a lot more … [Read more…]

Posted in: Angular, AngularJS, Asynchronous, Concurrency, JavaScript, Typescript, Web Development Tagged: async, await, JavaScript, multi-threaded, promise, single-threaded, threading, threads, Typescript

TimedProcessor – A slightly fancier timer

March 16, 2018 by Robert Leave a Comment

Timers are pretty useful.  They allow you to define a block of code that will execute on a given interval.  It’s the C# version of JavaScript’s setInterval.  There’s a lot of different reasons for using a timer, such as persisting updates to a database.  Since database calls can be slow, maybe you want to build … [Read more…]

Posted in: .NET Core, Asynchronous, C#, Concurrency Tagged: async, Asynchronous, C#, setInterval, Timer

ConcurrentHashSet

November 3, 2017 by Robert Leave a Comment

System.Collections.Concurrent provides a bunch of thread-safe collections that you can use when doing asynchronous programming without worrying about bad stuff happening. The simplest ones, ConcurrentBag<T> (which allows you to put stuff in and take things out in an unordered way) and ConcurrentDictionary<TKey, TValue> (a key/value set) are simple and easy to use, but there isn’t … [Read more…]

Posted in: Asynchronous, C#, Concurrency Tagged: Ansychronous, async, C#, Concurrent, ConcurrentBag, ConcurrentDictionary, ConcurrentHashSet, Thread Safe

Recent Posts

  • Allowing trailing JSON commas in ASP.NET Core 3
  • YADNC3JSG–Yet Another .NET Core 3.0 JSON Serializer Gotcha
  • Logging in a .NET Core 3 SignalR client
  • .NET Core 3 unit testing project sdk
  • .NET Core 3.0 Upgrade–New JSON Serializer Gotchas

Recent Comments

  1. Robert on C# Setting Socket Keep-Alive
  2. Oliver Schramm on C# Setting Socket Keep-Alive
  3. John Anderson on Waiting for a keypress asynchronously in a C# console app
  4. Ronald Garlit on .NET Core 3 unit testing project sdk
  5. Ronald Garlit on .NET Core 3 unit testing project sdk

Copyright © 2025 Darchuk.NET.

Omega WordPress Theme by ThemeHall