Skip to content

Instantly share code, notes, and snippets.

View noseratio's full-sized avatar

Andrew Nosenko noseratio

View GitHub Profile
using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.Generic;
namespace ConsoleApplication
{
public class Program
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace ProductsApp.Controllers
{
@noseratio
noseratio / TaskExt.Delay.cs
Created February 27, 2015 03:31
Implementing Task.Delay using a custom awaiter and unmanaged CreateTimerQueueTimer
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication
{
// by noseratio - stackoverflow.com/users/1768303/noseratio
class TaskExt
{
// http://stackoverflow.com/q/31495411/1768303
// use: await Task.Delay(5000, token.CreateAsynCancellationToken());
public static CancellationToken CreateAsynCancellationToken(this CancellationToken @this)
{
var cts = new CancellationTokenSource();
var reg = default(CancellationTokenRegistration);
reg = @this.Register(() =>
{
ThreadPool.QueueUserWorkItem(_ =>
// http://stackoverflow.com/q/31522642/1768303
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
// http://stackoverflow.com/questions/32412426/unexpected-stack-overflow-despite-yielding
public static class TaskExt
{
public static AlwaysAsyncAwaiter<T> AlwaysAsync<T>(this Task<T> @this) { return new AlwaysAsyncAwaiter<T>(@this); }
public struct AlwaysAsyncAwaiter<T> : System.Runtime.CompilerServices.ICriticalNotifyCompletion
{
readonly Task<T> _task;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace UnobservedTaskExceptionDemo
{
class Program
{
static void Main(string[] args)
{
@noseratio
noseratio / make-msbuild-shim.ps1
Last active May 2, 2023 11:31
A workaround for "node-gyp is unable to find msbuild if VS2019 is installed": https://github.com/nodejs/node-gyp/issues/1663
<#
This is a workaround for "node-gyp is unable to find msbuild if VS2019 is installed"
https://github.com/nodejs/node-gyp/issues/1663
It create a shim EXE as "MSBuild\15.0\Bin\MSBuild.exe" to target "MSBuild\Current\Bin\MSBuild.exe"
By noseratio - MIT license - use at your own risk!
It requires admin mode, I use wsudo/wsudox (https://chocolatey.org/packages/wsudo) for that:
wsudo powershell -f make-msbuild-shim.ps1
#>
#Requires -RunAsAdministrator
@noseratio
noseratio / Diags.cs
Created June 22, 2020 11:10
Double-click the output in Visual Studio Output Window to go to that source file and line
public static class Diags
{
public static void Log(
string message,
[System.Runtime.CompilerServices.CallerMemberName] string callerName = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0,
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "")
{
// trim trailing new line characters
var trimmedMessage = message.TrimEnd(Environment.NewLine.ToCharArray());
@noseratio
noseratio / TaskFromEvent.cs
Last active May 2, 2023 11:30
Wrap an event as Task
/// <summary>
/// Wrap an event as Task
/// </summary>
/// <see href="https://stackoverflow.com/a/22798789/1768303"/>
public static async Task<TEventArgs> FromEvent<TEventHandler, TEventArgs>(
Func<Action<TEventArgs>, Action, Action<Exception>, TEventHandler> getHandler,
Action<TEventHandler> subscribe,
Action<TEventHandler> unsubscribe,
Action<Action<TEventArgs>, Action, Action<Exception>> initiate,
CancellationToken token = default) where TEventHandler : Delegate