idisposable

How and when to dispose/garbage collect a singleton instance

别来无恙 提交于 2019-12-10 15:36:31
问题 I am using a Singleton instance created out of a nested class. This instance holds some static collections which are cleared when the Singleton is disposed, but the problem is I get a reference to non-null disposed Singleton which is not properly garbage collected. I would like to know WHEN and HOW to completely dispose and garbage collect my Singleton instance so that when the instance is queried again after dispose (and setting to null) a new Instance is created. I am using the following

When should a ManualResetEvent be disposed?

百般思念 提交于 2019-12-10 14:17:11
问题 I'm using an application that synchronizes threads using ManualResetEvent. FxCop told me to dispose those objects. I found the following discussion which told me the same: Do I need to Dispose() or Close() an EventWaitHandle? But I don't know when to dispose an instance of a ManualResetEvent. The following simplified code demonstrates the problem: private void btn_Click(object sender, EventArgs e) { var mre = new ManualResetEvent(false); new Thread(() => this.SetEvent(mre)).Start(); for (int

How should I ensure disposal of possibly disposable objects?

微笑、不失礼 提交于 2019-12-10 13:35:39
问题 I am working on a .NET project, which needs to interact with some user defined classes - reffered to as "jobs". All job classes must implement a specific interface IJob in order order for the library to consume them. Sometimes a job class might hold unmanaged resource, which needs to be explicitly disposed. How should I ensure that all jobs are properly disposed after use, if I do not know in advance if the job needs explicit disposal? I have a few ideas myself, but would like to hear your

Implementing IDisposable (the Disposable Pattern) as a service (class member)

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:15:25
问题 The Disposable pattern is one that is re-implemented on a per class basis. So, I was looking for a way to generalize it. The problem I ran into a few years ago is that, even if you implement it as class itself, you can't have an object derive from both your Disposable implementation and from another class (C# doesn't support multi-inheritance). The question is, how do you make a generic way to have the Disposable pattern implemented so you don't need to write it explicitly per class that

How is Dispose called on ADO.NET objects?

岁酱吖の 提交于 2019-12-10 10:50:40
问题 The database access classes that implement IDbConnection , IDbCommand and IDataReader all implement IDisposable , but obviously the Command and the Reader are dependent on the Connection. My question is, do I have to Dispose() of each these objects individually or will disposing of the Connection object dispose of the others too ? That is, can I do this and guarantee I'm not risking leaving any unmanaged resources not being freed: using (IDbConnection conn = GetConnection()) { IDbCommand cmd

DRY IDisposable Pattern

余生颓废 提交于 2019-12-10 10:06:34
问题 A lot of my classes repeat the below code to implement IDisposable. This seems to violate the DRY (Don't Repeat Yourself) principle. I could avoid some of the work by creating an AbstractDisposable base class, but that seems inappropriate / wouldn't work if I needed to extend other existing objects (assuming those objects weren't themselves disposable). Another option would be to use a template/meta language where I could specify lists of managed and unmanaged resources for each class and

Adding event handler in main() for SerialPort

坚强是说给别人听的谎言 提交于 2019-12-10 05:13:39
问题 I try to subscribe a event handler to the data received event. Seems like I cant specify the event handler function name. I dont understand why myComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); is giving me error message. Here is the problem, hope anyone can answer it. a busy cat http://img827.imageshack.us/img827/5904/20120125102247.png a busy cat http://img444.imageshack.us/img444/3855/20120125102202.png namespace serialport { public class Program {

Disposing MemoryCache in Finalizer throws AccessViolationException

六月ゝ 毕业季﹏ 提交于 2019-12-10 03:36:38
问题 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

Writing our own Dispose method instead of using Idisposable

醉酒当歌 提交于 2019-12-10 03:36:30
问题 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

async/await and the IDisposable interface

自古美人都是妖i 提交于 2019-12-10 03:06:17
问题 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