How to dispose objects having asynchronous methods called?

后端 未结 7 2145
再見小時候
再見小時候 2020-12-18 21:34

I have this object PreloadClient which implements IDisposable, I want to dispose it, but after the asynchronous methods finish their call... which

相关标签:
7条回答
  • 2020-12-18 22:37

    Well, disposing an object is used to kill resources that you don't want held until the GC (eventually) comes and collects your object. Does your dispose method kill anything you need in client_PreloadCompleted?

    You could have the object dispose itself, when all expected callbacks have happened: Keep a "reference counter" for each callback you are expecting and decrement that on each callback that happens - check for null at end of callback handler and dispose if so.

    Other workaround: Don't worry about IDisposable. GC will collect your object. You probably don't want a callback handler (that might not be fired) to have critical state. It (the callback) should just open any resources it needs when it is called and close them then.

    0 讨论(0)
提交回复
热议问题