Giter Site home page Giter Site logo

unity-threading's Introduction

unity-threading

Simple implementation to easily perform multithreaded asynchronous operations in Unity3D. All feedbacks, contributions are very much welcomed.

Example usages

Wait for a task to finish executing before returning back to main Unity thread

private void WaitAndReport()
{
    Async.Run(() =>
    {
        Thread.Sleep(1000);
    }).ContinueInMainThread(() =>
    {
        Debug.Log("Done waiting");
    });
}

Wait for a task to finish executing before returning a value back to main Unity thread

private void WaitAndReportValue()
{
    Async.Run(() =>
    {
        Thread.Sleep(1000);
        return 10;
    }).ContinueInMainThread((result) =>
    {
        Debug.Log("Done. Value is " + result);
    });
}

Perform a background operation then return the value to main thread once done. The background task is repeated every second (1000 ms)

private void CalcAndReportValueRepeatedly()
{
    Async.RunInBackground("CalcAndReportValueRepeatedly", 1000, () =>
    {
        long value = DateTime.UtcNow.Ticks;
        return value;
    }).ContinueInMainThread((result) =>
    {
        DateTime dateTime = DateTime.FromBinary(result);
        Debug.Log("DateTime value: " + dateTime);
    });
}

Execute a background task, enqueue the results into a threadsafe action queue to be printed out once the background task is done. The background task is repeated every second (1000 ms)

private readonly Queue<Action> _actionQueue = new Queue<Action>();
public Queue<Action> ActionQueue
{
    get
    {
        // Async class provide lock object with unique keyword identifier
        // for thread-safe operations
        lock (Async.GetLock("ActionQueue"))
        {
            return _actionQueue;
        }
    }
}

private void CalcAndReportRepeatedly()
{
    Async.RunInBackground("CalcAndReportRepeatedly", 1000, () =>
    {
        long date1 = DateTime.UtcNow.Ticks;
        Thread.Sleep(50);
        long date2 = DateTime.UtcNow.Ticks;
        ActionQueue.Enqueue(() =>
        {
            Debug.Log("date1: " + date1 + " + date2: " + date2 + " = " + (date1 + date2));
        });
    }).ContinueInMainThread(() =>
    {
        Action action = ActionQueue.Dequeue();
        action();
    });
}

Note

Call Dispatcher.Instance.Reset() to abort and clear all scheduled background or threaded tasks if needed. For example, it's useful when your application restart.

unity-threading's People

Watchers

 avatar  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.