idisposable

How to dispose objects having asynchronous methods called?

跟風遠走 提交于 2019-12-18 05:41:29
问题 I have this object PreloadClient which implements IDisposable , I want to dispose it, but after the asynchronous methods finish their call... which is not happening private void Preload(SlideHandler slide) { using(PreloadClient client = new PreloadClient()) { client.PreloadCompleted += client_PreloadCompleted; client.Preload(slide); } // Here client is disposed immediately } private void client_PreloadCompleted(object sender, SlidePreloadCompletedEventArgs e) { // this is method is called

Why is it always necessary to implement IDisposable on an object that has an IDisposable member?

给你一囗甜甜゛ 提交于 2019-12-18 05:15:09
问题 From what I can tell, it is an accepted rule that if you have a class A that has a member m that is IDisposable, A should implement IDisposable and it should call m.Dispose() inside of it. I can't find a satisfying reason why this is the case. I understand the rule that if you have unmanaged resources, you should provide a finalizer along with IDisposable so that if the user doesn't explicitly call Dispose, the finalizer will still clean up during GC. However, with that rule in place, it

Is there a list of common object that implement IDisposable for the using statement?

╄→гoц情女王★ 提交于 2019-12-18 04:33:29
问题 I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection , MemoryStream , etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket. Anything like that exist? If not, maybe we should make one. 回答1: Microsoft FxCop has a rule checking that you use an IDisposbale in a using block. 回答2: Perhaps

Session containing items implementing IDisposable

痴心易碎 提交于 2019-12-18 04:12:55
问题 In ASP.NET if items are left in the session state that Implement IDisposable but are never specifically removed and disposed by the application when the session expires will Dispose be called on the objects that any code in Dipose() will execute? 回答1: If the IDisposable pattern is implemented properly, then yes (i.e. the class's destructor will take care of disposing the object). I don't believe the ASP.NET session manager makes any guarantees about explicitly calling Dispose() on classes

Types that own disposable fields should be disposable. how to solve this warning?

我的未来我决定 提交于 2019-12-18 03:41:37
问题 I tried using Run Code Analysis option in VisualStudio 2012 , as a result of it I got a warning as CA1001 Types that own disposable fields should be disposable Implement IDisposable on 'DBConnectivity' because it creates members of the following IDisposable types: 'SqlConnection', 'SqlCommand'. I referred some question in SO, but I couldn't catch the point regarding IDisposable and following is the class, responsible for this warning. class DBConnectivity { public SqlConnection connection =

How does one tell if an IDisposable object reference is disposed?

Deadly 提交于 2019-12-18 03:00:48
问题 Is there a method, or some other light-weight way, to check if a reference is to a disposed object? P.S. - This is just a curiousity (sleep well, not in production code). Yes, I know I can catch the ObjectDisposedException upon trying to access a member of the object. 回答1: No - default implementation of IDisposable pattern does not support it 回答2: System.Windows.Forms.Control has an IsDisposed property which is set to true after Dispose() is called. In your own IDisposable objects, you can

What is the difference between managed and native resources when disposing? (.NET)

时光怂恿深爱的人放手 提交于 2019-12-17 22:27:18
问题 I was reading the MSDN article about how to implement IDisposable and I am uncertain about the difference between managed and native resources cited in the article. I have a class that must dispose 2 of its fields when it is disposed. Should I treat them as Managed (dispose only when disposing = true) or Native resources? 回答1: A managed resource is another managed type, which implements IDisposable . You need to call Dispose() on any other IDisposable type you use. Native resources are

Intercepting an exception inside IDisposable.Dispose

爷,独闯天下 提交于 2019-12-17 22:09:41
问题 In the IDisposable.Dispose method is there a way to figure out if an exception is being thrown? using (MyWrapper wrapper = new MyWrapper()) { throw new Exception("Bad error."); } If an exception is thrown in the using statement I want to know about it when the IDisposable object is disposed. 回答1: No , there is no way to do this in the .Net framework, you cannot figure out the current-exception-which-is-being-thrown in a finally clause. See this post on my blog, for a comparison with a similar

If I replace an image in a PictureBox control, should I dispose the original image first? .Net Winforms

白昼怎懂夜的黑 提交于 2019-12-17 20:20:37
问题 Following on from my question here, if I replace an image in a picture box, should I dispose the original image first? Or, what about this situation: Dim bm As New Bitmap(32,32) bm = New Bitmap(32,32) bm = New Bitmap(32,32) bm = New Bitmap(32,32) Does bm need only to be disposed at the end, or should it be disposed before each re-creation? Thanks all for the answers. A big oversight there on my part. I knew a control took care of disposing its children but It hadn't occurred to me that I

IOC containers and IDisposable

梦想与她 提交于 2019-12-17 19:23:51
问题 It was recommended to me that, when using an IOC container, I should change this: class Foobar: IFoobar, IDisposable {}; Into this: interface IFoobar: IDisposable{}; class Foobar : IFoobar{}; I'm wondering if this is ok, or if it solves one problem and creates another. It certainly solves the problem where I badly want to do this: using( IFoobar = myContainer.Resolve<IFoobar>() ) { ... } And now I know that any substitute won't cause a run-time error. On the other hand, now all my mock