Giter Site home page Giter Site logo

Comments (2)

thomaseyde avatar thomaseyde commented on May 20, 2024

After some investigation, I found this event handler to do the trick:

AppDomain.CurrentDomain.UnhandledException

I'm not sure if this handler catches all exceptions, or if we need to use TaskManager.UnobservedTaskException as well.

from fluentscheduler.

tallesl avatar tallesl commented on May 20, 2024

When you () => { } the library creates a thread and calls your action on it. If any exception is raised the library is able to catch it and tell you on UnobservedTaskException.

When you async () => { } the library creates a thread that then calls your action which, for being asynchronous, spawns another thread. Since the library don't await your action (it just calls it), the unhandled exception in this second thread crashes the application and you're only able to get a last glance at it on UnhandledException of AppDomain before everything falls down.

Here's a little illustration of what's going on:

static async Task Boom()
{
    throw new Exception("BOOOM!");
}

static void Main(string[] args)
{
    // Bye-bye message
    AppDomain.CurrentDomain.UnhandledException += (sender, e) => Console.WriteLine("Aww, we crashed...");

    Action synchronous = () => Boom().Wait();
    Action asynchronous = async () => await Boom();

    // This one prints "Gotcha!"
    try { synchronous(); }
    catch { Console.WriteLine("Gotcha!"); }

    // This one doesn't print "Gotcha!" and crashes the application
    try { asynchronous(); }
    catch { Console.WriteLine("Gotcha!"); }

    Thread.Sleep(Timeout.Infinite);
}

But don't worry, fixing your example is rather simple:

Schedule(() => {
    var faulty= new FaultyComponent().Run(); // Here the faulty task starts to run on its own thread
    // Do some other stuff
    faulty.Wait(); // Here we join back the faulty task, if any exception were raised you're gonna get it wrapped in an AggregateException
}).ToRunOnceIn(1).Seconds();

from fluentscheduler.

Related Issues (20)

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.