Coder Social home page Coder Social logo

libtasknet's Introduction

LibTaskNet, a libtask style coroutine library for .NET

LibTaskNet is a coroutine library inspired by libtask. Originally intented to have an API similar to that of libtask, LibTaskNet now uses the new C# await keyword to enable switching between functions. Instead of providing IO functions, the new .NET 4.5 Async methods can be used. This enables a co-routine that is supported by standard .NET APIs. Viewed another way, you can write Node.JS style apps without the layers of nested callback function definitions.

Check the fibers branch for a version of this library that uses fibers and is much more like libtask.

Example

A super-simple HTTP server that processes each request in its own coroutine and every second prints "waiting".

    private static async Task handleRequest(HttpListenerContext ctx)
    {
        ctx.Response.ContentType = "text/plain";
        var os = ctx.Response.OutputStream;
        var bytes = Encoding.ASCII.GetBytes("Hello World");
        await os.WriteAsync(bytes, 0, bytes.Length);
        ctx.Response.Close();
    }

    private static async Task httpServer()
    {
        CoopScheduler.AddTask(async () =>
        {
            while (true)
            {
                await Task.Delay(1000);
                Console.WriteLine("waiting");
            }
        });

        HttpListener l = new HttpListener();
        l.Prefixes.Add("http://+:8080/");
        l.Start();
        while (true)
        {
            var ctx = await l.GetContextAsync();
            CoopScheduler.AddTask(() => handleRequest(ctx));
        }
    }

    static void Main(string[] args)
    {
        CoopScheduler.AddTask(httpServer);
        CoopScheduler.StartScheduler();
    }

Dependencies

  • .NET Framework 4.5

Credits

This work is based in large part on the blog entry Await, SynchronizationContext, and Console Apps by Stephen Toub.

License

LibTaskNet is licensed under the three-clause BSD license.

libtasknet's People

Contributors

austinwise avatar

Watchers

oskark avatar James Cloos 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.