Giter Site home page Giter Site logo

episource / unblocker Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 75 KB

A .net framework library to execute most code - including blocking system calls - asynchronously, with the ability to cancel the invocation at any time.

License: Apache License 2.0

C# 100.00%
dotnet async pinvoke process tpl task

unblocker's Introduction

episource.unblocker

A .net framework library to execute most* code - including blocking system calls - asynchronously with the ability to cancel the invocation at any time.

The invocation request is handled by an isolated child process. In case it has cancellation support built-in, it is used. If an attempt to cancel the an ongoing execution does not succeed within a given timeout (or cancellation is not supported at all), the execution is aborted in an crash-consistent manner:

  1. An attempt is made to unload the corresponding AppDomain
  2. If that fails, the chid process handling the invocation request is killed

Worker processes are created on demand and are reused for subsequent invocation requests. Processes are recycled if not used for a configurable amount of time.

For all this to work you must only stick to the following rules:

  • The invocation request must refer to serializable objects, only (invocation target, arguments, result)
  • Make sure you don't unblock any invocation that change global state in a way that is not safe when killed in the middle (database without transactions, files, ...)

Quick start

All you need to do is create an instance of Unblocker and pass any method call lambda expression to its InvokeAsync method.

Examples

Managed sleep

Cancel managed code that blocks.

var unblocker = new Unblocker();var cts = new CancellationTokenSource();
var sleepTask = unblocker.InvokeAsync(ct => Thread.Sleep(1000), cts.Token);

Thread.Sleep(100);
cts.Cancel();

try {
    await sleepTask;
} catch (TaskCanceledException) {
    Console.WriteLine("Sleep cancelled!");
}

Native sleep

This works also when doing p/invoke!

public static class Native
{
    [DllImport("kernel32")]
    public static extern void Sleep(uint dwMilliseconds);
}var unblocker = new Unblocker();var cts = new CancellationTokenSource();
var sleepTask = unblocker.InvokeAsync(ct => Native.Sleep(1000), cts.Token);

Thread.Sleep(100);
cts.Cancel();

try {
    await sleepTask;
} catch (TaskCanceledException) {
    Console.WriteLine("Sleep cancelled!");
}

Return values

Return values also possible. Must be serializable, though.

[Serializable]
public class Job {
    private readonly int returnValue;
    
    public Job(int returnValue) {
        this.returnValue = returnValue;
    }

    public int GetValue() {
        return this.returnValue;
    } 
}var unblocker = new Unblocker();var j = new Job(10);
var val = await unblocker.InvokeAsync(ct => j.GetValue());
Console.WriteLine("Result: " + val);

unblocker's People

Contributors

episource avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.