State of Objects...

  • Thread starter Thread starter tascienu
  • Start date Start date
T

tascienu

Can anybody explain to me what happens in these scenarios:

- Dim m As myClass
- Dim m As New myClass
- m.Dispose()
- m.Dispose(True)
- m = Nothing
- MyChildClass inherits myClass -> will it also inherit Dispose() from
myClass or do i have to write a new one?

Also, since I am using this in a window service, which of the options
below will better conserve the memory...


' IN MY CLASS....
Class MyClass
Sub DoStuff()
Do until false
' stuff stuff stuff...
System.Threading.Thread.Sleep(TenSeconds)
Loop
End Sub

Sub DoStuff_version2()
' stuff stufff stuff
End Sub
End Class



' IN WINDOW SERVICE APP...

' Option 1
Sub TimerElapsed()
Timer.AutoReset = false '<-- timer runs only once...
MyClass.DoStuff() '<-- will loop forever until stopped...
MyClass = nothing...
end Sub

' Option 2
Sub TimerElapsed()
Timer.AutoReset = true ' <-- runs at every interval...
MyClass.DoStuff_Version2() ' <-- runs once every interval...
MyClass = nothing
End Sub
 
First off, if your class implements idosposable (you can't just create a
dispose sub, you also need to implement idisposable and implement
idisposable.dispose at the sub), then inherited classes should inherit the
dispose implementation. However, if your child class creates any objects, it
will need its own dispose implmentation which at some point calls the base
class's dispose through mybase.dispose()

On the second issue: it took a minute to understand what you were hinting at,

If I understand your question, you want to know whether it's better to use a
timer and call the subroutine with every timer tick, or whether it's better
to use sleep to let the thread sleep for 10 seconds.

Personally, I'm against using things like "exit do", "exit for", "goto", and
"sleep" unless it's absolutely necessary. The proper structured programming
style would be to use the timer to call your "dostuff()" and get out of the
loop as soon as it's complete. This is not only simpler, but also has a
clear-cut beginning and ending.

Your first method of leaving an infinite loop running will get you in
trouble. Not only will your program not properly handle events, but I can see
some other possible problems as well. Use the timer for what it was intended
for.
 
Back
Top