Skip to content

Instantly share code, notes, and snippets.

View AArnott's full-sized avatar

Andrew Arnott AArnott

View GitHub Profile
@AArnott
AArnott / EnumerableCache.cs
Created May 26, 2009 23:09
Get the benefit of deferred execution from generator methods without the cost of multiple generation when enumerated twice
//-----------------------------------------------------------------------
// <copyright file="EnumerableCache.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// This code is released under the Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------
namespace IEnumeratorCache {
using System;
using System.Collections;
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="MSBuildAcceleratorModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="MSBuildAcceleratorModelStoreContainer">
<EntitySet Name="File" EntityType="MSBuildAcceleratorModel.Store.File" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Project" EntityType="MSBuildAcceleratorModel.Store.Project" store:Type="Tables" Schema="dbo" />
@AArnott
AArnott / MSBuildAwaiterSample.cs
Created July 15, 2011 14:57
Demonstrates the simple extension method required to enable the C# 5 await keyword for MSBuild builds
namespace MSBuildAwaiterSample
{
using System.Threading.Tasks;
using Microsoft.Build.Execution;
class Program
{
static void Main(string[] args)
{
@AArnott
AArnott / WaitHandlerAwaitable.cs
Created July 15, 2011 15:55
C# 5 Awaitable WaitHandle
public static class AwaitExtensions
{
/// <summary>
/// Provides await functionality for ordinary <see cref="WaitHandle"/>s.
/// </summary>
/// <param name="handle">The handle to wait on.</param>
/// <returns>The awaiter.</returns>
public static TaskAwaiter GetAwaiter(this WaitHandle handle)
{
Contract.Requires<ArgumentNullException>(handle != null);
@AArnott
AArnott / MemoryModelVolatile.cs
Created June 25, 2012 04:23
Demonstrates how the memory model in .NET requires the use of volatile fields when not using locks.
using System;
using System.Threading;
namespace MemoryModelTest {
class Program {
static Barrier barrier;
static volatile LazyExample example;
static bool exit;
static volatile CountdownEvent evt;
@AArnott
AArnott / gist:3180390
Created July 26, 2012 05:15
Awaitable TaskScheduler
/// <summary>
/// Gets an awaitable object that immediately schedules any continuations on the given scheduler.
/// </summary>
/// <param name="scheduler">The scheduler to use for continuations.</param>
/// <returns>An awaitable object.</returns>
internal static TaskSchedulerAwaiter GetAwaiter(this TaskScheduler scheduler)
{
Contract.Requires<ArgumentNullException>(scheduler != null);
return new TaskSchedulerAwaiter(scheduler);
@AArnott
AArnott / ApmExtensions.cs
Last active May 1, 2020 21:40
Converts .NET TPL async task pattern to the APM pattern. See [Stephen Toub's blog post](http://blogs.msdn.com/b/pfxteam/archive/2011/06/27/10179452.aspx)
// LICENSED under the Ms-PL (http://opensource.org/licenses/MS-PL)
public static Task<TResult> ToApm<TResult>(this Task<TResult> task, AsyncCallback callback, object state) {
if (task == null) {
throw new ArgumentNullException("task");
}
if (task.AsyncState == state) {
if (callback != null) {
task.ContinueWith(
@AArnott
AArnott / FlickrNetAsync.cs
Last active December 23, 2015 15:09
Allows calling Flickr.NET (http://flickrnet.codeplex.com/) async APIs using the C# 5 await syntax.
private static Task<T> DoAsync<T>(Action<Action<FlickrNet.FlickrResult<T>>> begin) {
var tcs = new TaskCompletionSource<T>();
begin(result => {
if (result.HasError) {
tcs.SetException(result.Error);
} else {
tcs.SetResult(result.Result);
}
});
@AArnott
AArnott / ActivityBase.cs
Created February 11, 2014 15:53
A C# Xamarin.Android activity base class that offers StartActivityForResultAsync as a much simpler means to kick off activities and resume execution when they are done.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
public abstract class ActivityBase : Activity {
private int activityResultRegistrationCounter = 10000;
@AArnott
AArnott / ObservableCollectionAdapter.cs
Created March 12, 2014 19:19
A Xamarin.Android class that provides data binding from an ObservableCollection<T> to a ListView in the form of a BaseAdapter<T>.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;