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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:00:22+00:00 2026-06-02T03:00:22+00:00

present code is currently structured as follows: System.Timers.Timer myTimer; public void FirstMethod() { myTimer;=

  • 0

present code is currently structured as follows:

System.Timers.Timer myTimer;

public void FirstMethod() {
  myTimer;= new System.Timers.Timer();
  myTimer.start();
  SecondMethod();
}

public void SecondMethod(){
  //several things happen here and then
  myTimer.stop();  
}

I’ve been advised that I could use using to correctly garbage collect the Timer object. So I’ve tried to apply something like the following to my code (taken from here):

using (SomeClass someClass = new SomeClass())
{  
someClass.DoSomething();  
}  

I assume the following will error because myTimer is not known by SecondMethod()?

public void FirstMethod() {
  using (System.Timers.Timer myTimer = new System.Timers.Timer())   
  { 
  myTimer.start();
  SecondMethod();
  }
}

public void SecondMethod(){
//several things happen here and then
myTimer.stop();  
}
  • 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-02T03:00:23+00:00Added an answer on June 2, 2026 at 3:00 am

    You wrap an object that implements the IDisposable interface in a using block when it makes sense to do so. In this case it does not because the object must be valid at a higher scope. Remember, a using statement is just shorthand (syntactic sugar) for this:

    var myDisposableObj = new Whatever();
    try
    {
        // use myDisposableObj.  If an exception is thrown
        // in here somewhere the call to Dispose() still occurs.
    }
    finally
    {
        myDisposableObj.Dispose();
    }
    

    In your case you need to ensure that you call Dispose() on the object when you are done with it (and in a manner that accounts for exceptions which may be thrown that would prevent the call to Dispose() from taking place). You need the Timer to stick around for a while, so a using block is out of the question.

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

Sidebar

Related Questions

I am currently implementing a timer/callback system using Don Clugston's fastdelegates. (see http://www.codeproject.com/KB/cpp/FastDelegate.aspx )
Currently I am using the following code to present a UIAlertView: UIAlertView *alert =
Below I present you some code which has completely been butchered by me. $(.gig).hover(function()
here the following code is used to view the present modal view controller. [[self
I am using this code to check that array is present in the HashMap
My present contract engagement is at a large E-Commerce company. Their code base which
I'm writing code to check if there is a network connection present. In a
What is analog in datamapper for .present? method? I have code from rails app
I'm a novice, so I guess in the code I'm about to present, ther're
I'm currently attempting to track down the source of heap corruption in our code

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.