Click here to Skip to main content
14,241,167 members
   

C#

 
QuestionCatching exceptions in fire&forget tasks Pin
Bernhard Hiller14-Jun-19 2:58
memberBernhard Hiller14-Jun-19 2:58 
AnswerRe: Catching exceptions in fire&forget tasks Pin
BillWoodruff14-Jun-19 11:58
mveBillWoodruff14-Jun-19 11:58 
AnswerRe: Catching exceptions in fire&forget tasks Pin
Eddy Vluggen14-Jun-19 13:37
mveEddy Vluggen14-Jun-19 13:37 
GeneralRe: Catching exceptions in fire&forget tasks Pin
Bernhard Hiller16-Jun-19 20:49
memberBernhard Hiller16-Jun-19 20:49 
QuestionRe: Catching exceptions in fire&forget tasks Pin
Eddy Vluggen16-Jun-19 22:45
mveEddy Vluggen16-Jun-19 22:45 
AnswerRe: Catching exceptions in fire&forget tasks Pin
Gerry Schmitz14-Jun-19 20:59
mveGerry Schmitz14-Jun-19 20:59 
GeneralRe: Catching exceptions in fire&forget tasks Pin
Bernhard Hiller16-Jun-19 20:53
memberBernhard Hiller16-Jun-19 20:53 
AnswerRe: Catching exceptions in fire&forget tasks Pin
jschell15-Jun-19 3:56
memberjschell15-Jun-19 3:56 
Bernhard Hiller wrote:
and makes sure that exceptions do not crash the application


Depending on your definition of terms that is impossible in C#.

There are at least three exceptions which will cause a .Net AppDomain to exit and there is absolutely nothing you can do to stop it. One of those is the stack overflow exception. You can in fact 'catch' it but it will just keep going once the catch block exits. And it terminates the AppDomain not the process.

"Starting with the .NET Framework 2.0, you can't catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default."

StackOverflowException Class (System) | Microsoft Docs[^]

Now of course the workarounds for that depends on your definition.
1. Your main AppDomain (every process) could spin up a new AppDomain to do the actual launch. Then the 'application' can't exit only the new AppDomin will. Well at least not from this task.
2. Make sure the possible exceptions do not occur.


Bernhard Hiller wrote:
And then the service crashes wi


For starters looks like you are missing try/catches and logging.

But other than that there can however be any number of reasons for that. For example consider the following scenario.
1. You have class A in DLL X.
2. You use A in your main service class in a method M()
3. You call M().
4. Nothing else in X is used until M() is called.

In the above X is not loaded until M() is called but it is loaded when M() is invoked.
Now if X is not available you are going to get an exception.
Something similar happens if you have a class C where attributes/implements uses C or where the methods have a return type or parameter that refers to C.

So the best 'safe' way to create a service is as follows.

Create a class, call it Everything, which does ALL of the functionality of the application. That means that the service class, the one with OnStart() does NOT do any application functionality.

The service class looks like the following pseudo code

class MyService
{
    // It MUST be 'Object' and not 'Everything'
    // There should be NO other attributes and NO
    // other methods except ones that call directly 
    // to Everything.
    Object instance
    Logger logger = ...

    OnStart()
    {
       try
       {
           instance = new Everything();
           ((Everything)instance).Start();
       }
       catch(Exception e)
       {
           logger.Error("Failed to start", e);
       }
    }

    OnStop()
    {
       try
       {
           ((Everything)instance).Stop();
       }
       catch(Exception e)
       {
           logger.Error("Failed to stop", e);
       }
    }
}


Of course even that is limited because 'Logger' is also a dependency that can fail to load.
GeneralRe: Catching exceptions in fire&forget tasks Pin
Bernhard Hiller16-Jun-19 20:59
memberBernhard Hiller16-Jun-19 20:59 
GeneralRe: Catching exceptions in fire&forget tasks Pin
jschell23-Jun-19 5:42
memberjschell23-Jun-19 5:42 
Questionsome bugs don't retire Pin
BillWoodruff12-Jun-19 13:40
mveBillWoodruff12-Jun-19 13:40 
AnswerRe: some bugs don't retire Pin
Dave Kreskowiak12-Jun-19 14:52
mveDave Kreskowiak12-Jun-19 14:52 
GeneralRe: some bugs don't retire Pin
#realJSOP13-Jun-19 7:46
mve#realJSOP13-Jun-19 7:46 
GeneralRe: some bugs don't retire Pin
Dave Kreskowiak13-Jun-19 10:02
mveDave Kreskowiak13-Jun-19 10:02 
GeneralRe: some bugs don't retire Pin
BillWoodruff18-Jun-19 17:59
mveBillWoodruff18-Jun-19 17:59 
QuestionProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
Member 1449598412-Jun-19 2:46
memberMember 1449598412-Jun-19 2:46 
AnswerRe: ProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
OriginalGriff12-Jun-19 4:05
protectorOriginalGriff12-Jun-19 4:05 
GeneralRe: ProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
BillWoodruff12-Jun-19 4:57
mveBillWoodruff12-Jun-19 4:57 
AnswerRe: ProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
#realJSOP12-Jun-19 5:03
mve#realJSOP12-Jun-19 5:03 
AnswerRe: ProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
Gerry Schmitz12-Jun-19 6:15
mveGerry Schmitz12-Jun-19 6:15 
GeneralRe: ProcessId from ShDocVw.InternetExplorer(ShDocVw.ShellDocs) Pin
Eddy Vluggen12-Jun-19 11:12
mveEddy Vluggen12-Jun-19 11:12 
QuestionDraw vertical bold lines between some columns in datagridview Pin
henryvuong10-Jun-19 15:48
memberhenryvuong10-Jun-19 15:48 
AnswerRe: Draw vertical bold lines between some columns in datagridview Pin
OriginalGriff10-Jun-19 22:43
protectorOriginalGriff10-Jun-19 22:43 
GeneralRe: Draw vertical bold lines between some columns in datagridview Pin
henryvuong11-Jun-19 12:34
memberhenryvuong11-Jun-19 12:34 
GeneralRe: Draw vertical bold lines between some columns in datagridview Pin
OriginalGriff11-Jun-19 20:06
protectorOriginalGriff11-Jun-19 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.