I am using threaded timer which is creating new instance and call a method like below,
Timer_tick()
{
Car C;
C=new Car();
C.Start() /// this will take time to complete
}
My question is, there is another thread coming with next timer tick and perform same operation.
Then the second thread remove the reference from first Car instance & it will call the start method on new Car instance, since first object does not finished his start method (still in progress) but it doesn’t have the “C” variable reference anymore.
Will this create any problem to first instance Start() method completeness or execution.
Can it be collected by GC?
The
Carinstance is local to that particular call of theTimer_tick()function. Subsequent calls by other threads operate on their own instances of theCarobject.However, it doesn’t look like you’re doing anything with the car after the function exits, it will eventually get garbage collected without having done anything to it besides running the start function. Is that what you intended?