Multiple Instances of a class
-
I create a class using the System.Timers and the FileWatcher. The on the main form I have a global variable to that class. Called m_FileMon. In a command button I instance a new copy of that class. Every time I click the command button I get a new instance even though I only have one variable for that class. Does anyone have any idead why this is happening. Is this because the system timers or the Filesystemwatch are multithreaded? Any help would be greatly appriciated. Forever Developing
-
I create a class using the System.Timers and the FileWatcher. The on the main form I have a global variable to that class. Called m_FileMon. In a command button I instance a new copy of that class. Every time I click the command button I get a new instance even though I only have one variable for that class. Does anyone have any idead why this is happening. Is this because the system timers or the Filesystemwatch are multithreaded? Any help would be greatly appriciated. Forever Developing
Because you asked for one? What is it that you are trying to accomplish?
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Unknown wrote: "I love long walks, especialy taken by those that annoy me." Paraphrased from TMNT: "Cricket? You have to know what a crumpet is to understand Cricket."
-
I create a class using the System.Timers and the FileWatcher. The on the main form I have a global variable to that class. Called m_FileMon. In a command button I instance a new copy of that class. Every time I click the command button I get a new instance even though I only have one variable for that class. Does anyone have any idead why this is happening. Is this because the system timers or the Filesystemwatch are multithreaded? Any help would be greatly appriciated. Forever Developing
Here hope this helps clarify things a bit more. (Psudo code to follow) Public Class frmMain 'This the standard windows form code Private m_FileMon as clsMyFileMon Private sub_cmdStart_Click m_FileMon = new clsMyFileMon End Sub End Class Public Class clsMyFileMon Imports System.IO Imports System.Timers Imports System.Diagnostics Public Sub New 'Starting new timers and a new instance of 'the filesystem watcher. Using AddHandler 'to add event handlers for the timer_tick event and 'the filesystemwatcher created event. End sub End Class 'I am watching for the creation of files in a specific folder 'then after a given amount of time I delete those files End Class So here is the problem. Every time I click cmdStart it is starting another instance of my class. I know this because for every click of cmdStart I am getting another set of timers. The old instance is not being cleaned up. I have tried to kill the old instance by m_FileMon = Nothing, but the Finalize event does not fire. Is this because the timers and the filesystemwatch are executing on different threads? Do I need to implement IDispose to unhook the timers from my object and force the garbage collector to kill my class? Any help would be greatly appriciated. Forever Developing