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 7405383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:23:45+00:00 2026-05-29T05:23:45+00:00

Are Timers in .NET safe to abandon without calling Dispose() or Close()? static System.Timers.Timer

  • 0

Are Timers in .NET safe to abandon without calling Dispose() or Close()?

static System.Timers.Timer timer = new Timer();

void Main()
{
    timer.Elapsed += LogTimer_Elapsed(object, System.Timers.ElapsedEventArgs);
    timer.Start();
    Thread.Sleep(10000); // Simulate doing something on main thread
}

static void LogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    DoStuff();
}

Anyone see any problems with this solution?

static QueueLogger()
{
    LogQueue = new Queue<KeyValuePair<Logger, LogEntry>>(50);
    LogTimer = new Timer();
    LogTimer.Elapsed +=new System.Timers.ElapsedEventHandler(LogTimer_Elapsed);
    AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}

static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
    LogTimer.Stop();
    LogTimer.Dispose();
    LogTimer_Elapsed(sender, null);  // This is to process any remaining messages in the queue
}

static void LogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    lock (_locker)
    {
        while (LogQueue.Count > 0)
        {
            var queuedLogger = LogQueue.Dequeue();
            try
            {
                if (e != null) queuedLogger.Value.Message += " From " + sender.ToString();
                queuedLogger.Key.Log(queuedLogger.Value);
            }
            catch (Exception ex)
            {
                OnLoggingError(queuedLogger.Key, "Async Logging error", ex);
            }
        }
    }
}
  • 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-05-29T05:23:46+00:00Added an answer on May 29, 2026 at 5:23 am

    Galford,

    Yes, you can create new event which will fire if your application is about to exit e.g. OnExit and listen to the event when you application’s main thread is about to close. Once that is done you can create event logic and do your timer, business or clean up logic in OnExit event. Hope this helps.

    Regards

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

Sidebar

Related Questions

I use a System.Timers.Timer in my Asp.Net application and I need to use the
I have a .NET Windows Service (.NET 3.5) with a timer ( System.Timers.Timer ).
I used System.Timers.timer in global.as in asp.net to set a timer for scheduling execute
Possible Duplicate: System.Timer elapsed event seems to fire late for short intervals in .Net
VB.NET 2010, .NET 4 Hello all, I have a System.Timers.Timer object that does some
I have a process, which creates a dynamic list of timers(System.Threading.Timer) and continues to
Is it safe to use System.Threading.Timer in windows service running for a long time?
If you have an ASP.NET UpdatePanel and and ASP.NET Timer that trigers theUpdatePanel to
I am aware that in .NET there are three timer types (see Comparing the
I have a timer on a page in ASP.NET. After a certain period of

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.