idisposable

How to Dispose ManualResetEvent

戏子无情 提交于 2020-07-20 09:10:54
问题 Hi When i use following code: myManualResetEvent.Dispose(); Compiler gives this error: 'System.Threading.WaitHandle.Dispose(bool)' is inaccessible due to its protection level. howevr following line works fine: ((IDisposable)myManualResetEvent).Dispose(); is it the correct way to dispose or at runtime it might crash in some scenerios. Thanks. 回答1: The designers of the .NET Base Class Library decided to implement the Dispose method using explicit interface implementation: private void

How Does Autofac Handle Non-Disposable Components

核能气质少年 提交于 2020-06-29 04:32:08
问题 I have read that Autofac does a good job of disposing IDisposable resources, but I'm having trouble finding any info on what it does with components that do not have any unmanaged resources, and therefore do not need to implement IDisposable . I would assume these all get garbage collected, but is there any documentation on this scenario? EDIT Asking more specifically, are all Autofac-resolved components required to implement IDisposable ? The docs state that To take advantage of automatic

IDisposable, does it really matter

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-27 04:52:06
问题 Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and implement Dispose patterns in my classes containing disposable objects. However, in my environment I'm more or less the only one doing this. Others just don't understand what I'm doing and think my code is more difficult to understand. They just create database connections, open streams etc without calling Close or Dispose

Disposing of unmanaged objects in c#

左心房为你撑大大i 提交于 2020-02-05 05:50:20
问题 Let's say I have a class that has a MyCustomDatabaseAccess as a data member. MyCustomDatabaseAccess has a Dispose() method. MyCustomDatabaseAccess is the middleware class that accesses the database. public class MyClass { private MyCustomDatabaseAccess db_access; } Does MyClass need to implement IDisposable interface? My solution right now is to have do something like this: public class MyClass { private MyCustomDatabaseAccess db_access; public void GetDBResults () { db_access = new

Does this implementation of the Entity Framework leaks memory?

霸气de小男生 提交于 2020-01-25 12:23:34
问题 I just can't make out if the entity context is disposed in the usage flow when used in a using statement in a web application or a console application. Thanks! using System; using System.Web; namespace Foo.Model { public partial class FooEntities : ObjectContext { private const string CurrentContextKey = "FooEntities.Current"; [ThreadStatic] private static FooEntities _currentOnThreadStatic; private FooEntities _previousContext; /// <summary> /// Gets the current <see cref="FooEntities"/>

Why isn't SqlConnection disposed/closed?

核能气质少年 提交于 2020-01-24 05:14:34
问题 Given the method: internal static DataSet SelectDataSet(String commandText, DataBaseEnum dataBase) { var dataset = new DataSet(); SqlConnection sqlc = dataBase == DataBaseEnum.ZipCodeDb ? new SqlConnection(ConfigurationManager.AppSettings["ZipcodeDB"]) : new SqlConnection(ConfigurationManager.AppSettings["WeatherDB"]); SqlCommand sqlcmd = sqlc.CreateCommand(); sqlcmd.CommandText = commandText; var adapter = new SqlDataAdapter(sqlcmd.CommandText, sqlc); adapter.Fill(dataset); return dataset; }

Does C# app exit automatically dispose managed resources?

故事扮演 提交于 2020-01-23 06:16:13
问题 I am fully aware that using statements are the way to handle IDisposables. Please do not repeat this advice in comments. When a C# .NET 4.5 (or higher) application closes, what happens to the IDisposables which were not properly disposed? I know some have a finalizer for disposing unmanaged resources. But let's say I have a console app, with a static Stream variable. Is it disposed when I close the console app? What about a HttpClient? And how do you know in which situations it does and in

Error: Do not override object.Finalize. Instead, provide a destructor

一笑奈何 提交于 2020-01-21 16:13:31
问题 Getting the above error in following code. How to rectify it. Thanks. Please look for protected override void Finalize() { Dispose(false); } in the below code. using Microsoft.Win32; using System.Runtime.InteropServices; public class Kiosk : IDisposable { #region "IDisposable" // Implementing IDisposable since it might be possible for // someone to forget to cause the unhook to occur. I didn't really // see any problems with this in testing, but since the SDK says // you should do it, then

Error: Do not override object.Finalize. Instead, provide a destructor

懵懂的女人 提交于 2020-01-21 16:13:09
问题 Getting the above error in following code. How to rectify it. Thanks. Please look for protected override void Finalize() { Dispose(false); } in the below code. using Microsoft.Win32; using System.Runtime.InteropServices; public class Kiosk : IDisposable { #region "IDisposable" // Implementing IDisposable since it might be possible for // someone to forget to cause the unhook to occur. I didn't really // see any problems with this in testing, but since the SDK says // you should do it, then

Handling ObjectDisposedException correctly in an IDisposable class hierarchy [closed]

前提是你 提交于 2020-01-20 21:43:46
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . When implementing IDisposable correctly, most implementations, including the framework guidelines, suggest including a private bool disposed; member in order to safely allow multiple calls to Dispose() , Dispose(bool) as well as to throw ObjectDisposedException when appropriate.