This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Creates a custom object based on the json values. | |
/// </summary> | |
/// <typeparam name="T">The object type to convert.</typeparam> | |
public abstract class JsonCreationConverter<T> : JsonConverter | |
{ | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
if (reader.TokenType == JsonToken.Null) | |
return null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Future<T> { | |
private resolver: (value: T) => void; // readonly | |
private rejecter: (error: Error) => void; // readonly | |
private _isCompleted: boolean = false; | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolver = resolve; | |
this.rejecter = reject; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
/// <summary> | |
/// A base class for disposables that need exactly-once semantics in a threadsafe way. All disposals of this instance block until the disposal is complete. | |
/// </summary> | |
/// <remarks> | |
/// <para>If <see cref="Dispose()"/> is called multiple times, only the first call will execute the disposal code. Other calls to <see cref="Dispose()"/> will wait for the disposal to complete.</para> | |
/// </remarks> | |
public sealed class AnonymousDisposable : IDisposable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// The more evil version. :) | |
class Program | |
{ | |
static void Main(string[] args) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class AwaitableMessages | |
{ | |
public static Task<T> NextMessageAsync<T>() | |
{ | |
var tcs = new TaskCompletionSource<T>(); | |
Messenger.Default.Register<T>(null, item => tcs.TrySetResult(item)); | |
return tcs.Task; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
[TestClass] | |
public class TaskRunCancellationTokenUnitTests | |
{ | |
[TestMethod] | |
public void CancellationTokenPassedToSynchronousTaskRun_CancelsTaskWithTaskCanceledException() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
[TestClass] | |
public class WaitCanTreatInnerExceptionAsOperationCanceledException | |
{ | |
[TestMethod] | |
public void CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
[TestClass] | |
public class AwaitCanJustCatchOperationCanceledException | |
{ | |
[TestMethod] | |
public async Task CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I'm starting 10 calls at every 50 msec. Each call has a random execution time between 1-19 seconds. | |
// Many times the last answer comes from the 9th call instead of the 10th. | |
// I'm not sure I'm testing it properly. | |
void Main() | |
{ | |
var rand = new Random(); | |
Enumerable.Range(1,10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
[TestClass] | |
public class StartNewCancellationTokenUnitTests | |
{ | |
[TestMethod] | |
public void CancellationTokenPassedToStartNew_CancelsTaskWithTaskCanceledException() |