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

C#

 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Dave Kreskowiak18-Jun-19 9:24
mveDave Kreskowiak18-Jun-19 9:24 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Member 1370632018-Jun-19 18:17
memberMember 1370632018-Jun-19 18:17 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Dave Kreskowiak19-Jun-19 2:59
mveDave Kreskowiak19-Jun-19 2:59 
AnswerRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Eddy Vluggen15-Jun-19 1:32
mveEddy Vluggen15-Jun-19 1:32 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Member 1370632015-Jun-19 2:33
memberMember 1370632015-Jun-19 2:33 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Eddy Vluggen15-Jun-19 2:48
mveEddy Vluggen15-Jun-19 2:48 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Member 1370632015-Jun-19 3:20
memberMember 1370632015-Jun-19 3:20 
QuestionCatching exceptions in fire&forget tasks Pin
Bernhard Hiller14-Jun-19 2:58
memberBernhard Hiller14-Jun-19 2:58 
Weekend is approaching, and in case you may feel bored without a complicated programming task, I have a puzzle for you.
I already asked at StackOverflow: c# - Logging exceptions in fire&forget tasks - Stack Overflow[^], but did not get a usable answer.

In short, I want to create a method which takes an Action parameter, fires off a Task with it, and makes sure that exceptions do not crash the application (but logs the exception messages so that I can find out that something bad happened).

I tried following code:
public static void CreateAndStartTaskWithErrorLogging(Action _action, string _componentName, string _originalStacktrace = null)
{
    DateTime started = HighPrecisionClock.Now;
    Task task = new Task(_action);
    task.ContinueWith(_continuation => _continuation.LogExceptions(_componentName, started, _originalStacktrace));
    task.ConfigureAwait(false); // tried also without this line
    task.Start();
}
internal static void LogExceptions(this Task _t, string _componentName, DateTime _started, string _originalStacktrace = null)
{
    try
    {
        _t.Wait(1000);
    }
    catch (Exception ex)
    {
        Logger.LogError(_componentName, $"An exception occurred in a fire-and-forget task which was started at {_started}.\r\n" +
                                        $"The original stack trace is:\r\n{_originalStacktrace}");
        Logger.LogException(_componentName, ex);
    }
    // tried also without this block
    try
    {
        _t.Dispose();
    }
    catch (Exception dex)
    {
        Logger.LogException(_componentName, dex);
    }
}
Well, normally, it works.
Normally.
Not always...
And then the service crashes with an error entry in the Windows Event Log.
(That's also true for the solution in the first answer at SO).

Some more background.
The application is a highly configurable Windows Service. The function is called with Utilities.TaskExtensions.CreateAndStartTaskWithErrorLogging(() => DataStore.StoreSyncedData(data), Name);, where DataStore is set to a composite which in turn calls Parallel.ForEach(m_InnerDataStores, _store => { _store.StoreSyncedData(_syncedData); }); on its members. One of them writes a video with the Accord library, which sometimes causes an AccessViolation at <Module>.avcodec_encode_video2(libffmpeg.AVCodecContext*, libffmpeg.AVPacket*, libffmpeg.AVFrame*, Int32*), i.e. the exception may come from non-managed code. Perhaps 10% of these exceptions evade...

So my question is:
how should my function look like to catch those evading exceptions?

And extra upvotes if you can explain why my code (or the suggested solution at SO) fails.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

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

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.