GithubHelp home page GithubHelp logo

bobbycjones / ricaun.revit.ui.tasks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ricaun-io/ricaun.revit.ui.tasks

0.0 0.0 0.0 85 KB

Use Task to run Revit API code.

License: MIT License

Shell 8.12% C# 80.46% PowerShell 10.54% Batchfile 0.88%

ricaun.revit.ui.tasks's Introduction

ricaun.Revit.UI.Tasks

Revit 2017 Visual Studio 2022 Nuke License MIT Build Release

ricaun.Revit.UI.Tasks

ricaun.Revit.UI.Tasks package allows using Task to run Revit API code asynchronously and await the code to finish.

This project was generated by the ricaun.AppLoader Revit plugin.

RevitTaskService

The RevitTaskService is a service that manages the creation of the AsyncExternalEventHandler inside the Idling event.

UIApplication uiapp;
RevitTaskService revitTaskService = new RevitTaskService(uiapp);

or

UIControlledApplication application;
RevitTaskService revitTaskService = new RevitTaskService(application);

Initialize

Initialize the RevitTaskService to start the Idling event.

revitTaskService.Initialize();

Dispose

Dispose the RevitTaskService to stop the Idling event.

revitTaskService.Dispose();

IRevitTask

The RevitTaskService has the interface IRevitTask with the Run method to execute code in Revit Context.

IRevitTask revitTask = revitTaskService;

Run

The Run method runs the code inside Revit Context and await the code to finish with a CancellationToken.

CancellationToken token = CancellationToken.None;
UIApplication uiapp = await revitTask.Run((uiapp) =>
{
    // Code run inside Revit Context
    return uiapp;
}, token);

The IRevitTask interface has an extension methods for Run without UIApplication or CancellationToken and return.

await revitTask.Run(() => { });
await revitTask.Run(() => { return 1; });
await revitTask.Run((uiapp) => { });
await revitTask.Run((uiapp) => { return 1; });
await revitTask.Run(() => { }, token);
await revitTask.Run(() => { return 1; }, token);
await revitTask.Run((uiapp) => { }, token);

Example

Example sample to implement RevitTaskService within IExternalApplication.

public class App : IExternalApplication
{
    private static RevitTaskService revitTaskService;
    public static IRevitTask RevitTask => revitTaskService;
    public Result OnStartup(UIControlledApplication application)
    {
        revitTaskService = new RevitTaskService(application);
        revitTaskService.Initialize();

        Task.Run(async () =>
        {
            await Task.Delay(1000);
            await RevitTask.Run(() =>
            {
                TaskDialog.Show("Revit", "Hello from RevitTask");
            });
        });

        return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
        revitTaskService?.Dispose();

        return Result.Succeeded;
    }
}

Todo

  • Run code if in Revit Context instead of awaiting Task. - Done
  • CancellationToken timeout support to prevent infinite await. - Done

Similar Projects

There are some similar packages/implementations to run Revit API code in an async way using await/Tasks.

Release

License

This project is licensed under the MIT Licence.


Do you like this project? Please star this project on GitHub!

ricaun.revit.ui.tasks's People

Contributors

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