Domanda

I have an F# application that uses native libraries.

The code roughly looks like this

let exceptionHandler (sender:obj) (e:UnhandledExceptionEventArgs) =
    let msg = (e.ExceptionObject :?> Exception).Message
    printfn "An unhandled exception in \n Message:%s" msg
    Environment.Exit(1)

AppDomain.CurrentDomain.UnhandledException.AddHandler (new UnhandledExceptionEventHandler(exceptionHandler))



[|(*Some array*)|]
|> Array.map (fun (_) -> Task.Factory.StartNew(fun () ->
try
   (* a lot of native libraries calls  that can fail with a bunch of exceptions *)
with
   | ex -> printfn "%s" ex.Message))
|> Task.WaitAll

The 'Try` block executes in a loop and after some time (a couple of hours) I get "*.exe has stopped working" dialog, with the information that the faulting module is some native library that my code references.

Before I start with dump debugging: any ideas why my catch block didn't catch the exception, or why the UnhandledException handler didn't report anything? Any ideas how can I survive all the exceptions thrown by native libs (in this case probably a simple retry will solve the problem).

(Note I added C# tag because most probably this isn't solely F# related, but more a .NET issue).

(Note II, yes I am aware that try catch everything is not the best solution :))

È stato utile?

Soluzione 2

My suggestion: profile to see how much memory is consumed by one task, and then decide (based on anticipated memory available) how many you can run side by side.

this article gives advice on how to set a limit to number of concurrent threads in TPL

Altri suggerimenti

Some exceptions can't be caught, StackOverflowException is one of them.

.NET also allow to terminate a process using Environment.FailFast method

You will need to dump debugging to be sure of what is happening.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top