c# - Dangling reference during asynchronous operation -
currently working on xna project , pimped microsoft.xna.framework.audio.soundeffectinstance-class little creating wrapper provides events signal different states of sound (i.e. stopped, playing, paused).
now have following situation: create instance of class "eventsoundeffect" local reference, e.g.:
public void func(){ ... eventsoundeffect ese = new eventsoundeffect( /*some sound */); ... ese.stopped += callback; ese.playasync(); //method ends directly after ese.playasync() } private void callback(object sender, eventargs e){ // stuff } i realized event-firing using threadpool , while-loop. not nice, works. when state of sound changes, find in while-loop like
//pseudocode public void playasync(){ sound.play(); // starts playing sound asynchronously, returns (soundeffectinstance) threadpool.queueuserworkitem( obj => { while(...){ if(sound.soundstate == soundstate.stop) break; } if(soundstoppedevent != null) soundstoppedevent(this, new eventargs()); } } } now, happen gc strike in between end of func() , callback , collect object on heap?
local reference object lost after playasync(), thread threadpool still works on , reference show @ callback (sender). lead unforeseen consequences?
thank you
now, happen gc strike in between end of func() , callback , collect object on heap?
no, reference held inside threadpool, since use ("this") inside lambda-function.
Comments
Post a Comment