Jump to content
Xtreme .Net Talk

Keep system.threading.timer from being GC (garbage collected)


Recommended Posts

Posted

Does anyone know how to keep a threading.timer from being garbage collected? The timer works fine and mutiple threads are usually running without any problems, however, when the GC is executed the timers are disposed of. Anybody have any ideas?

 

Dim timerDelegate As Threading.TimerCallback = AddressOf Me.SendFaxResponse
checkInboxTimer = New System.Threading.Timer(timerDelegate, Nothing, 60000, 20000)

  • Administrators
Posted

Where is the above code being run from? (button click, form load etc.)

If the variables timerDelegate and checkInboxTimer are only declared within a single routine then they will go out of scope and be eligble for collection as soon as the routine exits.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

The code is being run from a class method and checkInboxTimer is a class variable. The class is question is instantiate in the form load of the main form.

What I getting from your post is that once the program exits the class the timer will be subject to GC.

 

How can I prevent this from happening?

  • Leaders
Posted

Simply put, you need to store a reference to the timer. It doesn't matter where. If there is a specific class that uses it then that class should keep a reference to it and as long as the class as referenced the timer is referenced.

 

The fail-safe method would be to store the timer in a static variable, which will never be garbage collected until the application closes.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...