开发者

How to Dispose() when class is running an async loop? Wait for loop to exit?

开发者 https://www.devze.com 2023-03-10 06:05 出处:网络
I have a class that asynchronously calls a method which contains a loop.This loop will run until the class is disposed.When implimenting Dispose() I already have the ability to tell the loop to stop (

I have a class that asynchronously calls a method which contains a loop. This loop will run until the class is disposed. When implimenting Dispose() I already have the ability to tell the loop to stop (and thus the async thread completes). After this call to stop the loop in Dispose() should I wait for the loop to complete and the thread to die or does it not matter? Given that I've signaled for the loop to complete, should I just continue t开发者_运维问答o exit the Dispose() call? What is the best practice here? My loop may take a few minutes to complete once signaled.


Some objects have a responsibility of ensuring that entities outside them get cleaned up. Such objects may be the only thing anywhere with the information and impetus necessary to perform such cleanup; if they don't do it, it won't happen. IDisposable exists to allow such objects to fulfill their responsibility.

Despite its name, IDisposable doesn't exist for the purpose of destroying objects. Rather, it exists to allow objects to put their affairs in order so that they may safely be abandoned (and eventually destroyed by the garbage-collector). It is entirely proper for an object's Dispose method to fulfill its responsibilities by handing them off to some other entity, provided that other entity can be trusted to fulfill them. That sounds essentially like what you're doing.

The only aspect that may be tricky in a situation like yours is deciding whether Dispose should simply set things in motion so things should get cleaned up, or whether it should stick around until they actually are. There are substantial arguments in favor of both approaches. Having a synchronous Dispose method is useful if the calling code needs the cleanup to have occurred before it can proceed, and may also be useful if something might go wrong in the cleanup process. Having Dispose throw an exception is ugly (especially since there's no clean pattern by which Dispose to avoid losing any pending exception if that occurs) but trapping an exception while the execution context still exists is much nicer than finding out after the fact that some error occurred at some unknown time while doing some unknown action. Unfortunately, using a synchronous dispose can slow down cleanup and may sometimes cause deadlock. Using an asynchronous dispose may improve responsiveness, but code which expects that Dispose will have made something like a file or I/O device available may be surprised if the device won't be available for some time after Dispose has returned. In some cases, it may be helpful to have an "Open" method check whether a dispose is in progress on a resource, and wait for it to complete if so.


If the loop is likely to be using managed resources that would end up being Disposed then you must wait for the loop to complete, then tidy up in your Dispose, then exit the Dispose method.


That's a question only you can answer; if the consuming code needs to assume that after calling Dipose that the loop will have stopped, then wait. If not, then don't.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号