idisposable

Can a ThreadStatic IDisposable be automatically disposed?

一个人想着一个人 提交于 2020-01-10 19:53:08
问题 This is not a question of how to automatically call dispose - my problem is the opposite: I have a thread pool where each thread has a ThreadStatic Graphics (which was created from an Image) to perform text size measuring. Now I've ran into the problem that from time to time the graphics seems to be disposed as even reading the TextRenderingHint property fails(causes an ArgumentException). Is there some mechanism which disposes the Graphics e.g. if the thread is idle for a long period? 回答1:

Why would a class implement IDisposable explicitly instead of implicitly?

别来无恙 提交于 2020-01-09 03:49:06
问题 I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); Why would the designer of a class such as this one choose to implement IDisposable explicitly? As Anthony Pegram says, doing things this way masks the fact that the object should be disposed

Disposable Context Object pattern

大憨熊 提交于 2020-01-06 04:04:44
问题 Introduction I just thought of a new design pattern. I'm wondering if it exists, and if not, why not (or why I shouldn't use it). I'm creating a game using an OpenGL. In OpenGL, you often want to "bind" things -- i.e., make them the current context for a little while, and then unbind them. For example, you might call glBegin(GL_TRIANGLES) then you draw some triangles, then call glEnd() . I like to indent all the stuff inbetween so it's clear where it starts and ends, but then my IDE likes to

Disposable Context Object pattern

[亡魂溺海] 提交于 2020-01-06 04:04:28
问题 Introduction I just thought of a new design pattern. I'm wondering if it exists, and if not, why not (or why I shouldn't use it). I'm creating a game using an OpenGL. In OpenGL, you often want to "bind" things -- i.e., make them the current context for a little while, and then unbind them. For example, you might call glBegin(GL_TRIANGLES) then you draw some triangles, then call glEnd() . I like to indent all the stuff inbetween so it's clear where it starts and ends, but then my IDE likes to

When/how is IDisposable.Dispose called?

丶灬走出姿态 提交于 2020-01-04 14:48:07
问题 Given the following class which implements both Dispose and IDisposable.Dispose : internal class DisposableClass : IDisposable { public void Dispose() { } void IDisposable.Dispose() { } } When I make a call to DisposeableClass.Dispose (through an instance of the class), the public void Dispose is called: DisposableClass newClass = new DisposableClass(); try { } finally { newClass.Dispose(); } If you change the try-finally to a using statement, IDisposable.Dispose is called. using

Is “Using{}” good practice to dispose object even if there is no variable referring to it

馋奶兔 提交于 2020-01-04 14:38:09
问题 In my company, I am seeing code like this using (LoggerFactory.GetTracer(_log.ModuleName + "._GetAccessTokenFromWns")) {...} When I looked up, I learned it disposes the objects referred by variables declared inside "Using" parenthesis. Does it also help for code like above, where there is no variable referring to the object returned by the factory? Thanks, 回答1: After I see your question I write a simple code like this: using (new FileStream("sample.txt",FileMode.CreateNew)) { } And then I

Why does VS2005/VB.NET implement the IDisposable interface with a Dispose(disposing as boolean) overload?

南笙酒味 提交于 2020-01-04 13:30:12
问题 Recently I needed to compare a suggested pattern for IDisposable and object finalization with the auto-generated one we which VS2005/VB.NET provide. We have used the auto-generated one a fair bit, but after looking it the two side by side I had a number of questions about the VB.NET implementation... For reference, here is the IDE's implementation: Public Class Class1 Implements IDisposable Private disposedValue As Boolean = False ''// To detect redundant calls ''// IDisposable Protected

Why does VS2005/VB.NET implement the IDisposable interface with a Dispose(disposing as boolean) overload?

狂风中的少年 提交于 2020-01-04 13:29:37
问题 Recently I needed to compare a suggested pattern for IDisposable and object finalization with the auto-generated one we which VS2005/VB.NET provide. We have used the auto-generated one a fair bit, but after looking it the two side by side I had a number of questions about the VB.NET implementation... For reference, here is the IDE's implementation: Public Class Class1 Implements IDisposable Private disposedValue As Boolean = False ''// To detect redundant calls ''// IDisposable Protected

VS warning: Disposable object created by 'disposable' is never disposed, why?

ε祈祈猫儿з 提交于 2020-01-04 05:09:21
问题 I am writing a C# application and have stumbled across something that makes me unsure if it is a Visual Studio bug, or my knowledge is incorrect. The culprit is a using statement inside "catch" part of a try/catch, where Visual Studio 2019 shows a warning that a disposable object will not be disposed: Disposable object created by 'MakeMeSomeDisposableObject()'s is never disposed The same statement used outside the catch, shows no warning message. Here is an example of what I am talking about:

Autofac, IDisposable and manually calling .Resolve in certain circumstances

混江龙づ霸主 提交于 2020-01-03 17:04:50
问题 I've posted a general guideline question when it comes to IDisposable objects and using Autofac here: Dependency Injection and IDisposable. Unfortunately, I did not account for one particular scenario in our project and it's really a separate question that stands on its own, so will ask it here: I have a Repository object that manages the life of a session object inside it. Thus, Repository object is IDisposable and destroys session (Repository is injected with a factory delegate at