0

In my console app I start a new background thread to process some long running operation. I have the following code

Task.Factory.StartNew(
        () =>
            {
                try
                {
                    // do some work
                }
                catch (MyException e)
                {
                    logger.LogError(e);
                    throw;
                }
            },
        cancellationToken,
        TaskCreationOptions.LongRunning,
        TaskScheduler.Default)
    .ConfigureAwait(false);

I need to catch any exception, log it and re-throw to the main thread. I run it in docker container, and after exception it should restart. In this case, an exception logged, but application still working. How to throw it up to main thread to stop application?

It is not duplicate, because I use TaskCreationOptions.LongRunning. It not work for TaskCreationOptions.LongRunning!!!

Pr.Dumbledor
  • 636
  • 1
  • 11
  • 29
  • 1
    It would be silly to re-throw it on the main thread. The exception didn't occur in the main thread. Either await the task or Invoke something that can handle the exception on the main thread and initiate a terminate of the program. – Jeroen van Langen Mar 10 '20 at 15:32
  • 1
    See: https://stackoverflow.com/questions/5983779/catch-exception-that-is-thrown-in-different-thread – Wyck Mar 10 '20 at 15:35
  • @JeroenvanLangen It is code of library, I need to throw exception to up. I can't await, because it should process in background – Pr.Dumbledor Mar 10 '20 at 15:37
  • 2
    @Wyck are you serious? You marked my question as a duplicate of c++ question? – Pr.Dumbledor Mar 10 '20 at 16:13
  • 2
    @PeterDuniho Why do you marked my question as a duplicate of c++ question? – Pr.Dumbledor Mar 10 '20 at 16:14
  • What does the main thread of your console app doing while the long-running task is working in the background? Is it waiting for user input with `Console.ReadLine` for example? – Theodor Zoulias Mar 10 '20 at 16:43
  • @Pr.Dumbledor: I didn't. I submitted the duplicate as https://stackoverflow.com/questions/5983779/catch-exception-that-is-thrown-in-different-thread, a C# question. But someone else had already included the C++ question, which I didn't notice, so it was listed as well. There was no reason to reopen the question though...this has been asked and answered dozens of times already. Please do some research before you post new questions. – Peter Duniho Mar 10 '20 at 16:51
  • 1
    This question should not be closed as a duplicate, until it becomes clear what the OP is actually asking. It *could* be closed for the reason of needing details or clarity, but this could be done later, if the OP neglected to respond to requests for clarity by updating their question. Closing questions hastily and for the wrong reason doesn't do any good to anyone. – Theodor Zoulias Mar 10 '20 at 17:19
  • 1
    IMO, you need to think about the problem in a different way. Don't say "re-throw." Instead say, "When an exception is caught in the background thread, I need to _send a message_ to the main thread, and I need the main thread to receive the message and report or deal with the problem." – Solomon Slow Mar 10 '20 at 17:30
  • 1
    @Pr.Dumbledor, I'm sorry about that. I pasted the wrong link when I marked as duplicate. I added the correct duplicate as a comment moments after. Blame Stack Overflow for not letting me revoke my oops. I apologize. – Wyck Mar 10 '20 at 18:07
  • @TheodorZoulias yes, I have something like `Console.ReadLine`. User can input some data and send it to process – Pr.Dumbledor Mar 10 '20 at 20:05
  • 1
    And while the user is inputting data, if at that time an exception occurs in the long-running background task, you want the console application to immediately crash with the exception thrown as an unhandled exception? Keep in mind that the `Console.ReadLine` is a blocking method, which means that the main thread is blocked until a line has been entered by the user. – Theodor Zoulias Mar 10 '20 at 21:17
  • Related: [Is there a good method in C# for throwing an exception on a given thread?](https://stackoverflow.com/questions/44656/is-there-a-good-method-in-c-sharp-for-throwing-an-exception-on-a-given-thread) TL;DR: No. – Theodor Zoulias Mar 10 '20 at 21:42

0 Answers0