Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:28+00:00 2026-06-05T12:07:28+00:00

I have windows service application (no winforms). In Main method I started timer. Timer

  • 0

I have windows service application (no winforms). In Main method I started timer. Timer elapsed event handler is running in new thread(?). Is any easy way how to throw exceptions from timer elapsed event handler back to main thread?

I was trying to handle exceptions in handler body and rise custom events, but when I restart the main process on rising this event, now runs 2 processes doing same things simultaneously.

How can I get event or exception information form timer event handler thread back to main thread?

Thank you.

EDIT:

using System;
using System.Security.Permissions;
using System.Timers;

namespace TestingConsoleApplication.Model
{
    static class ThreadExceptionTester
    {
    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
    public static void Run()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

        Timer Timer = new Timer(1000);
        Timer.Elapsed += new ElapsedEventHandler(TimerEventHandler);
        Timer.Start();

        try
        {
            throw new Exception("1");
        }
        catch (Exception e)
        {
            Console.WriteLine("Catch clause caught : " + e.Message);
        }

        //throw new Exception("2");
    }

    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        Exception e = (Exception)args.ExceptionObject;
        Console.WriteLine("MyHandler caught : " + e.Message);
    }

    static void TimerEventHandler(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Throwing from timer event handler");
        throw new Exception("timer exception");
    }
}
}

This write on console this:

Catch clause caught : 1
Throwing from timer event handler

And then program crash with unhandled exception on throw new Exception(“timer exception”); If I uncomment throw new Exception(“2”); Exeption is processed and on console is also “Catch clause caught : 2”. In other words, timer exceptions are not processed by MyHandler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:07:30+00:00Added an answer on June 5, 2026 at 12:07 pm

    You need to use AppDomain.UnhandledException event for subscribing to all exception events.

    EDIT:
    According to MSDN:

    In the .NET Framework version 2.0 and earlier, the Timer component
    catches and suppresses all exceptions thrown by event handlers for the
    Elapsed event. This behavior is subject to change in future releases
    of the .NET Framework.

    i have looked into source of System.Timers.Timer from .NET4 using dotPeek and there still no changes since 2.0, so consider using System.Threading.Timer instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows Service application which uses a Threading.Timer and a TimerCallback to
I have a Windows Service application (developed in C++) running under Local System account.
I have a windows service application, running under WinXPe, which sometimes fails with an
I have one Windows Service/Application running silently on the PC, when user starts any
Say, I have a Windows service application and also windowed client applications running in
I have a windows service application which works using remoting. It is used to
I have a c# windows service application that is crashing without throwing an exception
I have a simple windows service application I am trying to debug in VS
enter code here Hi All, I have a simple windows service application that connects
If you have a windows service and a windows forms application that uses the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.