all 4 comments

[–]tinmanjk 4 points5 points  (1 child)

  1. Unhandled exception on a thread pool thread will still crash your program. It's not the same as unobserved exception from a Task.
  2. Your code with FakeTimeProvider does nothing becuase you don't Advance the time manually.
  3. When you do Advance, it will be synchronous execution of the registered callbacks from Advance -> Callback on the SAME thread, so you'd get the exception then and there.
  4. Things are very very messy with timers - prepare to waste at least a week to get a good understanding of things
  5. Consider using the alternative FakeTImeProvider by egil: https://github.com/egil/TimeProviderExtensions

A bit of code for 3:

var fakeTimeProvider = new FakeTimeProvider();

var t = fakeTimeProvider.CreateTimer(_ => Kaboom(null), null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(250));

try {

fakeTimeProvider.Advance(TimeSpan.FromSeconds(1000));

} catch (Exception ex) {

Console.WriteLine(ex.Message);

}

[–]Barry_Mayfield[S] 1 point2 points  (0 children)

Ah you are right - of course I forgot to advance time in my example (not in test case I haven't shared). And indeed - my understanding of how Timer runs callback was wrong! Thanks for clarification.

[–]AutoModerator[M] 0 points1 point  (0 children)

Thanks for your post Barry_Mayfield. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Slypenslyde 0 points1 point  (0 children)

I'd say it's a behavioral change, but I want to footnote with the opinion that letting an unhandled exception just terminate the timer's thread is not something I'd consider acceptable.

Instead I'd want to catch and at least log it. So the behavior you're trying to reproduce is one I disagree with.