jayy66 Posted October 23, 2006 Posted October 23, 2006 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) Quote
Administrators PlausiblyDamp Posted October 23, 2006 Administrators Posted October 23, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jayy66 Posted October 23, 2006 Author Posted October 23, 2006 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? Quote
Leaders snarfblam Posted October 23, 2006 Leaders Posted October 23, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.