idisposable

The primary use of IDisposable interface [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-05 06:02:49
Possible Duplicate: Proper use of the IDisposable interface "IDisposable Interface" article tells: The primary use of this interface is to release unmanaged resources Why? Why only unmanaged? Whole my life I thought its PRIMIRALY use is to release ANY resources: managed (connections to DBs, services proxies, etc) and unmanaged (if they are used in application). P.S. I believe there are already questions on this topic, but can't find them. David Hoerster The underlying connections to db's are not managed, as are file handles and a number of other low-level o/s objects. They are unmanaged.

I don't quite understand the workings of using/Disposable objects

房东的猫 提交于 2019-12-05 06:01:13
I asked a question regarding returning a Disposable ( IDisposable ) object from a function , but I thought that I would muddle the discussion if I raised this question there. I created some sample code: class UsingTest { public class Disposable : IDisposable { public void Dispose() { var i = 0; i++; } } public static Disposable GetDisposable(bool error) { var obj = new Disposable(); if (error) throw new Exception("Error!"); return obj; } } I coded it this way deliberately, because then I call: using (var tmp = UsingTest.GetDisposable(true)) { } Using the debugger, I notice that the Dispose

using(IDisposable obj = new …) in C# to write code blocks in stream (e.g. XML)

前提是你 提交于 2019-12-05 05:50:50
I have started to use classes implementing IDisposable to write blocks in streams, with the using statement. This is helpful to keep a correct nesting and avoid missing or wrongly placed start/end parts. Basically, the constructor writes the start of a block (e.g. opening XML tag), Dispose() the end (e.g. closing XML tag). Example is the UsableXmlElement below (it's for large XMLs, so LINQ to XML or XmlDocument in memory are no options). However, these IDisposable do not implement the sophisticated pattern recommended by Microsoft, with Destructor/Finalizer, separate Dispose(bool) method and

Writing our own Dispose method instead of using Idisposable

心不动则不痛 提交于 2019-12-05 05:02:23
After going through a lot of articles on IDisposable , I got confused about its usage. All the articles explain what is it and how to implement it, but I want to understand what we will miss if we don't have it. Here is an example of an class implementing IDisposable . Often the use of dispose is shown as disposing a database connection. public class Test : IDisposable { public Test() { DatabaseConnection databaseConnection = new DatabaseConnection(); } public void Dispose() { if (this.databaseConnection != null) { this.databaseConnection.Dispose(); this.databaseConnection = null; } } }

async/await and the IDisposable interface

三世轮回 提交于 2019-12-05 03:07:16
I have a class which implements the IDisposable interface to dispose a private variable _MailMessage The same class has a async method that makes use of the private IDisposable variable, namely async public Task<bool> Send My question is: Will the normal IDisposable implementation dispose the private variable after the async method completes? Here is an example of the class I am talking about: public class Email : IEmail { private readonly IEmailData _EmailData; private MailMessage _MailMessage = new MailMessage(); public Email(IEmailData emailData) { if (emailData == null) { throw new

Disposing MemoryCache in Finalizer throws AccessViolationException

混江龙づ霸主 提交于 2019-12-05 03:01:34
EDIT See edit note at the bottom of the question for additional detail. Original question I have a CacheWrapper class which creates and holds onto an instance of the .NET MemoryCache class internally. MemoryCache hooks itself into AppDomain events, so it will never be garbage-collected unless it is explicitly disposed. You can verify this with the following code: Func<bool, WeakReference> create = disposed => { var cache = new MemoryCache("my cache"); if (disposed) { cache.Dispose(); } return new WeakReference(cache); }; // with false, we loop forever. With true, we exit var weakCache = create

Disposing the members that implement IDisposable

半城伤御伤魂 提交于 2019-12-05 02:38:53
In my Dispose methods (like the one below), everytime i want to call someObj.Dispose() i also have a check for someObj!=null. Is that because of bad design on my part? Is their a cleaner way to ascertain that Dispose of all the members (implementing IDisposable) being used in an object is called without having a risk of NullReference exception ? protected void Dispose(bool disposing) { if (disposing) { if (_splitTradePopupManager != null) { _splitTradePopupManager.Dispose(); } } } Thanks for your interest. Maybe someone else can chime in on this, but I don't personally think it's a design flaw

Form.ShowDialog() and dispose

最后都变了- 提交于 2019-12-05 02:02:51
If I have a method like this: public void Show() { Form1 f = new Form1(); f.ShowDialog(); } Do I still need to call dispose on the form even though it will go out of scope, which will be eligible for garbage collection. From some testing, calling this Show() multiple times .. at some point it seems like the GC collects it since I can see the memory spiking then it goes down at some point in time. From MSDN it seems to say you MUST call dispose when the form is not needed anymore. In your specific example, no, it's unlikely that it would be particularly useful. Forms do not hold onto a

Use of Process with using block [duplicate]

空扰寡人 提交于 2019-12-05 01:44:57
This question already has answers here : Closed 8 years ago . Possible Duplicate: What happens if I don't close a System.Diagnostics.Process in my C# console app? As System.Diagnostics.Process inherits from Component which implements IDisposable , should I always create a Process with a using block? For example, this...: using (var process = new Process()) { process.StartInfo.FileName = "some process.exe"; process.Start(); process.WaitForExit(); } ...instead of this: var process = new Process { StartInfo = { FileName = "some process.exe" } }; process.Start(); process.WaitForExit(); I ask

Why disposing StreamReader makes a stream unreadable? [duplicate]

本秂侑毒 提交于 2019-12-05 01:42:46
This question already has answers here : Can you keep a StreamReader from disposing the underlying stream? (8 answers) Closed 4 years ago . I need to read a stream two times, from start to end. But the following code throws an ObjectDisposedException: Cannot access a closed file exception. string fileToReadPath = @"<path here>"; using (FileStream fs = new FileStream(fileToReadPath, FileMode.Open)) { using (StreamReader reader = new StreamReader(fs)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } fs.Seek(0, SeekOrigin.Begin); // ObjectDisposedException thrown. using