Skip to content

Instantly share code, notes, and snippets.

View i-e-b's full-sized avatar
🤔
Thinking

Iain Ballard i-e-b

🤔
Thinking
View GitHub Profile
@i-e-b
i-e-b / RequestHook.cs
Created November 30, 2018 09:19
Hook into a Http Request's context, to set an action that will fire as that request is ending.
/// <summary>
/// Hooks into a Http Request's context, to set an action that
/// will fire as that request is ending.
/// <para></para>
/// When the hook action fires, the context will be closed, so no response
/// data can be sent, but clean-up or write-back actions within the server
/// will be honoured
/// </summary>
public class RequestHook
{
@i-e-b
i-e-b / WinForm_Suspender.cs
Created September 2, 2018 13:45
Stop and restart Windows form rendering
public static class ControlHelper {
public static void Suspend(Control c) {
if (c == null || c.IsDisposed || !c.IsHandleCreated) return;
Win32.SendMessage(c.Handle, 0x00B, (IntPtr)0, IntPtr.Zero);
}
public static void Resume(Control c) {
if (c == null || c.IsDisposed || !c.IsHandleCreated) return;
Win32.SendMessage(c.Handle, 0x00B, (IntPtr)1, IntPtr.Zero);
/// <summary>
/// Logarithmic record of good and bad calls per second
/// </summary>
internal class Timeslice
{
private readonly long[] sampleSum;
private readonly int[] sampleCount;
private readonly long ticksPerSample;
@i-e-b
i-e-b / SizeOfType.cs
Last active May 31, 2019 08:00
Get byte sizes of unknown primitive types based on an instance of that type
public static class Utils
{
public static int SizeOf<T>(T obj)
{
return SizeOfCache<T>.SizeOf;
}
private static class SizeOfCache<T>
{
// ReSharper disable once StaticMemberInGenericType
@i-e-b
i-e-b / ChildProcessTracker.cs
Created June 5, 2018 10:27
Kill child processes if the tracking parent dies
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ProxyParent
{
/// <summary>
/// Allows processes to be automatically killed if this parent process unexpectedly quits.
/// This feature requires Windows 8 or greater. On Windows 7, nothing is done.</summary>
@i-e-b
i-e-b / ReadLockedFiles.cs
Created May 22, 2018 12:40
Read from files even if they have exclusive range locks
using (var f = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var m = MemoryMappedFile.CreateFromFile(f, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true))
{
using (var s = m.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
{
using (var r = new StreamReader(s))
{
var l = r.ReadToEnd();
Console.WriteLine(l);
@i-e-b
i-e-b / Sync.cs
Created April 10, 2018 15:13
Helper to run C# async methods synchronously without deadlocking all the time.
/// <summary>
/// Helper class to properly wait for async tasks
/// </summary>
public static class Sync
{
private static readonly TaskFactory _taskFactory = new
TaskFactory(CancellationToken.None,
TaskCreationOptions.None,
TaskContinuationOptions.None,
TaskScheduler.Default);
@i-e-b
i-e-b / test_guid.txt
Created February 12, 2018 14:24
Test guid (easy to spot: foldable face beef feed classifiable)
F01DAB1E-FACE-BEEF-FEED-C1A551F1AB1E
@i-e-b
i-e-b / openSCAD.sh
Created January 15, 2018 09:18
How to start openSCAD in Ubuntu
QT_QPA_PLATFORMTHEME= openscad
@i-e-b
i-e-b / webClient_withDefaultProxy.cs
Last active June 3, 2019 13:12
Web proxy connections for C# web clients.
private static WebClient WebClientWithProxy_Net46() {
var client = new WebClient();
var defaultProxy = WebRequest.DefaultWebProxy;
if (defaultProxy == null) return client;
defaultProxy.Credentials = CredentialCache.DefaultCredentials;
client.Proxy = defaultProxy;
return client;
}