idisposable

What is the best way to cleanup the resources used by a Crystal Reports ReportDocument object?

霸气de小男生 提交于 2019-12-05 00:52:58
I am working on an application that uses Crystal Reports for the reporting. It opens a given report in a ReportDocument object, does what it needs to do and then closes the report. using (var report = OpenReport(reportSourceInfo)) { // Do stuff with the report report.Close(); } The OpenReport method does some validation of the source file and returns an open ReportDocument object. Testing has shown that this code does what it's meant to do and appears to have no issues. The problem I'm really after advice on is when I do a code analysis (CA) build of the reporting project, I get the following

How should I handle exceptions in my Dispose() method?

假装没事ソ 提交于 2019-12-05 00:44:11
I'd like to provide a class to manage creation and subsequent deletion of a temporary directory. Ideally, I'd like it to be usable in a using block to ensure that the directory gets deleted again regardless of how we leave the block: static void DoSomethingThatNeedsATemporaryDirectory() { using (var tempDir = new TemporaryDirectory()) { // Use the directory here... File.WriteAllText(Path.Combine(tempDir.Path, "example.txt"), "foo\nbar\nbaz\n"); // ... if (SomeCondition) { return; } if (SomethingIsWrong) { throw new Exception("This is an example of something going wrong."); } } // Regardless of

IEnumerator: Is it normal to have an empty Dispose method?

≡放荡痞女 提交于 2019-12-05 00:41:00
I'm writing an IEnumerator<T> class to iterate over a COM collection I'm wrappering . I've noticed that IEnumerator<T> extends IDisposable , so I'm required to implement the Dispose method. However, I can't think of anything I would put there, as I only have a reference to the collection (which I wouldn't want being disposed at the end of a foreach ), and an int for the index. Is it normal to leave the Dispose method empty? Yes, it is. IEnumerator<T> implements IDisposable in case you make an enumerator that does need to be disposed. Since most enumerators don't need to be disposed, the method

Singleton with finalizer but not IDisposable

别来无恙 提交于 2019-12-04 23:13:18
This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources: IDisposable is for cleaning up managed and unmanaged resources deterministically. Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee that they are cleaned up even if the client code does not call Dispose() on the instance. Classes that are responsible for managed resources only should never implement a finalizer. If you have a finalizer then you must implement IDisposable (this allows client

What to do with IObservers being disposed?

♀尐吖头ヾ 提交于 2019-12-04 22:35:31
问题 I'm using Reactive Extensions for easy event handling in my ViewModels (Silverlight and/or Wp7 apps). For sake of simplicity let's say I have a line like this in the ctor of my VM: Observable.FromEvent<PropertyChangedEventArgs>( h => MyObject.PropertyChanged += h, h => MyObject.PropertyChanged -= h) .Where(e=>e.PropertyName == "Title") .Throttle(TimeSpan.FromSeconds(0.5)) .Subscribe(e=>{/*do something*/}); this returns an IDisposable object, which if disposed will unsubscribe. (Am I right in

Shared ownership of IDisposable objects in C#

余生颓废 提交于 2019-12-04 21:40:46
Is there any classes in C# which provide shared ownership of IDisposable objects? Something like shared_ptr in c++? And if not, what are best practices here? UPDATE I'm writing a c++/cli wrapper over native lib. And I need release native resources ( MAPI COM interfaces for example, so I need determenistic resource releasing ). Native part: //Message.h class Message { ... }; //MessageSet.h class MessageSet { ... class iterator : public std::iterator<std::forward_iterator_tag, Message*> { ... public: Message* operator*(); bool operator!=(const iterator& that); iterator& operator++(); }; iterator

How to manage IDisposable Objects that are cached?

守給你的承諾、 提交于 2019-12-04 20:11:30
问题 I have an object that is expensive to create, which uses some unmanaged resources that must be explicitly freed when done with and so implement IDisposable(). I would like a cache for instance of these expensive resources so that the creation cost is minimized, but I am having trouble knowing how to deal with the disposal. If the methods that use the objects are responsible for the disposal then I end up with disposed instances in the cache, which then have to be recreated, defeating the

How does GC and IDispose work in C#?

雨燕双飞 提交于 2019-12-04 18:32:09
I remember i was loading in images by streaming it from the net straight into a bitmap. close the stream, return the bitmap and held it in an image control. I excepted when i did = loadPicture() the first bitmap would be freed like a smart pointer would do in C++. But it didnt and i consumed a lot of ram until i called dispose. So my question is. How does the GC and Dispose able objects work in C#? and why isnt it implemented like a smart_ptr? Marc Gravell References are not smart pointers. Letting a reference-variable go out of scope, replacing it with another value, and/or setting it with

How to implement IDisposable in Entity Framework?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:27:29
I am having my entity framework context in a separate EL Layer, which stands for Entity Layer and then I move to DAL, then BL and my user inteface aspx.cs code page. I am confused as such how to use IDisposable in the same. What I am doing till now, supopose in my DAL I have context of my entities. namespace abc { public class Action: IDisposable { Entities context = new Entities(); // all the methods public void Dispose() { context.Dispose(); } } } Is it the correct way of doing so? I am just a naive programmer so help me in learning the same logic. Personally I would change it a little bit,

Disabling/Fixing Code Analysis warnings from .Designer.cs files

落花浮王杯 提交于 2019-12-04 17:09:13
问题 I am using DataVisualization.Charting.Chart extensively, and for the most part it is working. However, I've been running Code Analysis frequently, and have all my own warnings taken care of. But, there are about 30 CA2000 (object not disposed along all exception paths) in the *.Designer.cs files that use the charting. The Designer files are generating pretty much all the charting code, and almost all the charting elements implement IDisposable . I have "Suppress results from generated code"