idisposable

Do I need to Dispose a SemaphoreSlim

非 Y 不嫁゛ 提交于 2019-12-10 00:44:44
问题 According to the documentation: "a SemaphoreSlim doesn't use a Windows kernel semaphore". Are there any special resources used by the SemaphoreSlim which make it important to call Dispose when the SemaphoreSlim will no longer be used? 回答1: Yes. It may use a ManualResetEvent that uses a SafeWaitHandle which is a SafeHandle and it has an unmanaged handle. You can see it in the reference source here. SafeHandle is finalizable so if you don't dispose of it (by disposing of the SemaphoreSlim ) it

Which .NET Framework classes implement IDisposable

☆樱花仙子☆ 提交于 2019-12-09 17:19:19
问题 Seems like this has to be documented somewhere but I'm not finding is anywhere. Perhaps my Google-fu is weakening. 回答1: You realize that this will depend on what you call .NET Framework classes . You probably might want to specify which assemblies you are looking for. Armed with this information you can load those assemblies and use reflection to list all public types that implement IDisposable in a given assembly. Let's take the System assembly as example: class Program { static void Main()

When to Dispose?

☆樱花仙子☆ 提交于 2019-12-08 19:16:06
问题 I'm getting confused about all this talk about IDispose and "using" Statements. I wonder if someone can tell me if I need to use either a "using" Statement or some sort of implementation of IDispose in the following test example... public class Main() { MyFile myFile = new MyFile("c:\subdir\subdir2\testFile.txt"); Console.Writeline("File Name: " + myFile.FileName() + "File Size: " + myFile.FileSize()); } public class MyFile { private FileInfo _fInfo; public MyFile(string fullFilePath) {

Unity to dispose of object

微笑、不失礼 提交于 2019-12-08 18:14:02
问题 Is there a way to make Unity dispose property-injected objects as part of the Teardown? The background is that I am working on an application that uses ASP.NET MVC 2, Unity and WCF. We have written our own MVC controller factory that uses unity to instantiate the controller and WCF proxies are injected using the [Dependency] attribute on public properties of the controller. At the end of the page life cycle the ReleaseController method of the controller factory is called and we call

Should WebException.Response.GetResponseStream() be close / disposed?

别来无恙 提交于 2019-12-08 17:58:26
问题 When I catch a .NET WebException , should I close / dispose the Response.GetResponseStream() ? The MSDN example does not close or dispose anything in the exception. Many SO answers recommend disposing the response and / or the stream. I disposed the stream and this caused big problems. Because GetResponseStream() (always? / sometimes?) returns the same instance. So when I get the response stream and then dispose it, then maybe rethrow the exception to another layer that also gets the response

Will System.Runtime.Caching.MemoryCache dispose IDisposable items when evicted?

て烟熏妆下的殇ゞ 提交于 2019-12-08 17:01:11
问题 I have a builder class that creates an instance implementing IDisposable. Whenever the item to build is already in the cache, the builder will return that instance instead. My question is, will the cache call the Dispose() method on the IDisposable items it contains when they are evicted or do I have to explicitly code that behavior on the callback CacheItemPolicy.RemovedCallback? 回答1: No Dispose is not called. It is easy to test. public class TestClass : IDisposable { public void Dispose() {

TcpClient and NetworkStream dispose problem

倖福魔咒の 提交于 2019-12-08 08:45:46
问题 I'm using this piece of code to process a connection to a server and read data from the client using(var client = _listener.EndAcceptTcpClient(ar)) { var clientStream = client.GetStream(); // Get the request message Messages.ReceiveMessage(clientStream, msg => ProcessRequest(msg, clientStream)); } Now, the ReceiveMessage method calls BeginRead() on the Stream passed as a parameter, but I'm getting an ObjectDisposedException. I know that a solution is to call stream.Dispose() when I don't need

IDisposable member of WPF Window class

孤街浪徒 提交于 2019-12-07 23:58:20
问题 When I add IDisposable class member to Windows Forms Form class, I add disposing code to Form's Dispose method. What should I do when I add IDisposable class member to WPF Window class, which is not IDisposable? 回答1: Extend your window class so that it has IDisposable, then implement the Dispose() method as before: public class MyWindow : Window, IDisposable { public void Dispose() { // Dispose your objects here as before. } } 回答2: Approaches you can use: Use Closed event on Window .

Correct way to pass Entity objects between layers?

二次信任 提交于 2019-12-07 18:08:31
问题 I am just learning the Entity Framework and have made some good progress on incorporating it with my layered code structure. I have 2 visual layers, a business layer and a data access layer. My problem is in passing entity object between layers. This code sample does not work: // BLL public static void Test1() { List<User> users = (from u in GetActiveUsers() where u.ID == 1 select u).ToList<User>(); // Do something with users } // DAL public static IQueryable<User> GetActiveUsers() { using

Understanding disposable objects

筅森魡賤 提交于 2019-12-07 17:26:51
问题 I've looked in SO about a question like this one, and even that I've found quite a few, any of those threw any light into this matter for me. Let's assume I have this code: public class SuperObject : IDisposable { public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } } Do I need the protected virtual void Dispose(bool) on SuperObject ? Since there is really nothing to dispose there. public interface ICustom : IDisposable { }