Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
thomaslevesque / AsyncStackTrace.cs
Created August 16, 2013 13:42
Extension method to add "logical" stack trace to async methods, with usage example. Copy/paste in Linqpad and add the following namespaces: - System.Runtime.CompilerServices - System.Threading.Tasks
async void Main()
{
try
{
await FooAsync().AsyncTrace();
}
catch(Exception ex)
{
ex.FullTrace().Dump();
}
public class Deferral : IDisposable
{
private TaskCompletionSource<object> _completionSource = new TaskCompletionSource<object>();
~Deferral()
{
Dispose(false);
}
public void Dispose()
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
namespace WpfApplication1
{
public class AsyncObservableCollection<T> : ObservableCollection<T>
{
private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
@thomaslevesque
thomaslevesque / CustomDragCursor.cs
Created May 2, 2014 22:55
Drag and drop with custom cursor
void Main()
{
var pb = new PictureBox { Image = Image.FromFile(@"D:\tmp\dotnet.png") };
bool down = false;
Cursor dragCursor = null;
pb.MouseDown += (sender, e) => down = true;
pb.MouseUp += (sender, e) => { down = false; dragCursor = null; };
pb.MouseMove += (sender, e) =>
{
if (down)
@thomaslevesque
thomaslevesque / BasicAuthenticationModule.cs
Last active August 29, 2015 14:01
A Basic authentication module that uses UTF-8 to encode username and password (rather than ANSI for the built-in implementation)
using System;
using System.Linq;
using System.Net;
using System.Text;
using Sharebox.Utils.Collections;
namespace Sharebox.Communication
{
class BasicAuthenticationModule : IAuthenticationModule
{
@thomaslevesque
thomaslevesque / Sleeper.cs
Created May 14, 2014 08:23
A cancellable synchronous Sleep implementation
using System;
using System.Threading;
namespace Utils.Threading
{
/// <summary>
/// A cancellable replacement for Thread.Sleep
/// </summary>
public static class Sleeper
{
@thomaslevesque
thomaslevesque / FullOuterJoinTests
Last active July 20, 2017 20:24
AssertThrowsWhenArgumentNull: helper to test correct validation of null arguments
using System;
using NUnit.Framework;
namespace MyLibrary.Tests.XEnumerableTests
{
[TestFixture]
class FullOuterJoinTests
{
[Test]
public void FullOuterJoin_Throws_If_Argument_Null()
@thomaslevesque
thomaslevesque / CallerInfo.cs
Last active August 29, 2015 14:09
Caller info attributes
using System;
namespace System.Runtime.CompilerServices
{
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute
{
}
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerFilePathAttribute : Attribute
@thomaslevesque
thomaslevesque / memset.cs
Last active July 6, 2021 01:48
Benchmark of memset implementations in C# using a loop or the initblk CLR instruction (run in LinqPad)
void Main()
{
var array = new byte[10000000];
var initBlk = new InitblkMemoryHelper();
var loop = new LoopMemoryHelper();
// First run for JIT warmup and type initialization
initBlk.Memset(array, 0, array.Length, 42);
loop.Memset(array, 0, array.Length, 42);
// Solution for James Michael Hare's anagram challenge (http://geekswithblogs.net/BlackRabbitCoder/archive/2015/07/28/little-puzzlersndashlist-all-anagrams-for-a-word.aspx)
// Run in LinqPad, adding a reference to Microsoft.Experimental.Collections
void Main()
{
string path = @"D:\tmp\words.txt";
var dict = new MultiValueDictionary<string, string>();
foreach (var word in File.ReadLines(path))
{
dict.Add(new string(word.OrderBy(c => c).ToArray()), word);
}