[英]Do I need to use IDisposable?
Trying to see if I'm understanding this correctly. 尝试查看我是否正确理解了这一点。 Regretfully, I have not had much experience with IDisposable.
遗憾的是,我没有使用IDisposable的丰富经验。 We're taking a static class and making it non-static, and it's a class that is responsible for making web service calls in our Xamarin app.
我们正在使用静态类,并使它成为非静态类,并且该类负责在Xamarin应用中进行Web服务调用。 This is an object that I would definitely not want lying around.
我绝对不希望这是一个对象。 So I thought of having this class inherit the IDisposable interface, and I implemented the methods as shown:
因此,我想到让此类继承IDisposable接口,并实现了如下所示的方法:
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
//~WebServiceCallThread()
//{
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
//}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
//GC.SuppressFinalize(this);
}
So all this appears to be doing, is when the Dispose
method is called, it then calls a virtual method declared above Dispose(bool disposing)
. 因此,所有这些似乎都在做,就是在调用
Dispose
方法时,它随后调用在Dispose(bool disposing)
上方声明的虚拟方法。 With the current code, this does nothing (or so it seems). 对于当前的代码,这什么也没做(看起来如此)。 It just calls the
Dispose
method, in which that calls an overloaded Dispose
method. 它只是调用
Dispose
方法,其中调用了重载的Dispose
方法。 But nothing is actually "disposing" here. 但实际上,这里没有任何东西可以“处置”。
Upon some research, it SEEMS (which is why I'm asking, I think I am understanding correctly). 经过一些研究,它看起来很像(这就是为什么我要问的原因,我认为我理解正确)。 It says to only call a
finalizer
and the GC.SuppressFinalize
method IF you dispose managed state
. 它说,如果您
dispose managed state
,则仅调用finalizer
和GC.SuppressFinalize
方法。 I'm not sure what dispose manage state
means, but there's really nothing in my class that needs to get disposed. 我不确定
dispose manage state
含义是什么,但是在我的课上确实没有什么需要配置的。 I just have some global int variables but that's it. 我只是有一些全局int变量,仅此而已。 The meat of this class is a HttpCall, which is already disposing of the objects automatically, as such:
此类的核心是HttpCall,它已经自动处理对象,例如:
public async void CallUrlHttpClient(string URL, System.Action<string> Handler, System.Action<XamarinMobile.Classes.ErrorInfo> ErrorHandler, int Tries)
{
var result = string.Empty;
didMakeSuccessfulHttpRequest = false;
using (var client = new HttpClient())
{
... Code
}
...
So here's my question: Do I need to make this class inherit IDisposable
? 所以这是我的问题:我是否需要使此类继承
IDisposable
? I'm wondering if anything under the hood is being done, but it seems as if my application can just utilize the GC to get rid of my object that uses this class. 我想知道是否在做任何事情,但似乎我的应用程序可以利用GC摆脱使用此类的对象。 This is how I call it, via a static method in a class:
这就是我通过类中的静态方法调用它的方式:
public static void WebServiceCall(string URL, System.Action<string> Callback)
{
//using (XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread())
//{
XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread();
thread.Call(URL, Callback);
//}
}
If I understand GC correctly, once this method is done, the GC will automatically get rid of the object. 如果我正确理解GC,则一旦完成此方法,GC就会自动摆脱该对象。 So do I really need to make the
WebServiceCallThread
inherit from IDisposable? 那么,我真的需要使
WebServiceCallThread
继承自IDisposable吗? Again I'm asking if I'm understanding all of this correctly. 我再次问我是否正确理解了所有这些。 If not, please correct me and let me know where I'm confused.
如果没有,请纠正我,让我知道我感到困惑的地方。
Thanks. 谢谢。
If your class created the HttpClient
and maintained that instance throughout its own lifetime (like an instance variable) then you would implement IDisposable
, and your Dispose()
method would dispose the HttpClient
. 如果您的类创建了
HttpClient
并在其整个生命周期中都维护了该实例(例如实例变量),则将实现IDisposable
,而Dispose()
方法将处理HttpClient
。
That would be this part: 那就是这一部分:
if (disposing)
{
// TODO: dispose managed state (managed objects).
_httpClient.Dispose();
}
In this case you don't need to implement IDisposable
because you're already disposing your HttpClient
. 在这种情况下,您不需要实现
IDisposable
因为您已经在处理HttpClient
。
using (var client = new HttpClient())
{
... Code
}
However, it's actually recommended to create one instance of HttpClient
per endpoint and reuse it instead of creating and disposing over and over. 但是, 实际上建议每个端点创建一个
HttpClient
实例HttpClient
用它,而不是一遍又一遍地创建和处理。 (If your load isn't that heavy then you likely won't see any bad effect from creating and disposing HttpClient
.) (如果您的负载不是那么重,那么创建和处理
HttpClient
可能不会造成任何不良影响。)
If you wanted to avoid creating and disposing HttpClient
then you could implement IDisposable
. 如果要避免创建和处理
HttpClient
则可以实现IDisposable
。 Your class either creates or receives (in its constructor) an instance of HttpClient
. 您的类创建或接收
HttpClient
实例(在其构造函数中)。 Now that your class "owns" that instance which needs to be disposed when your class is disposed, you make your class IDisposable
. 现在,您的类“拥有”了在处置您的类时需要处置的那个实例,您就可以使您的类
IDisposable
。 That way others know that your class needs to be disposed. 这样,其他人知道您的班级需要处置。 They might not know why, but that's not important.
他们可能不知道为什么,但这并不重要。 By making it
IDisposable
you're letting them know that they need to dispose it. 通过使其成为
IDisposable
您可以让他们知道他们需要处理它。 And then when your class is disposed it cleans up its HttpClient
. 然后,当您的类被处置时,它将清理其
HttpClient
。
Just to provide a little extra clarification: 只是提供一些额外的说明:
Many people, if asked "What is IDisposable
for?" 许多人被问到“
IDisposable
用途是什么?” will answer that it causes garbage collection. 会回答这会导致垃圾收集。 It actually has nothing to do with garbage collection.
实际上,它与垃圾回收无关。 Objects always get garbage collected when they're out of scope (there are no more references to them) whether or not they implement
IDisposable
. 无论对象是否实现
IDisposable
,它们总是在超出范围时(不再有对其的引用)被垃圾回收。
The confusion comes up because the recommended pattern for IDisposable
(which you referenced) includes a finalizer ( ~WebServiceCallThread()
) which is called when the object is garbage collected. 之所以会出现混乱,是因为
IDisposable
的推荐模式(您所引用的模式)包括一个终结器( ~WebServiceCallThread()
),该终结器在对象被垃圾回收时被调用。
When the garbage collector removes an item it calls the finalizer if there is one. 当垃圾收集器删除一项时,如果有一项,它将调用终结器。 This pattern causes the finalizer to call
Dispose()
if it hasn't been called already, just as a backup in case someone didn't call Dispose()
like they were supposed to. 如果尚未调用终结器,则此模式将使终结器调用
Dispose()
,以防万一有人不按预期调用Dispose()
情况下进行备份。 But it's no substitute for calling Dispose()
because there's no telling when the garbage collector will collect the item. 但这不能替代调用
Dispose()
因为没有告诉垃圾收集器何时收集该项目。 There's a chance the finalizer will never get called. 终结者有可能永远不会被调用。 It's just a backup to try to mitigate what happens if consumers don't call
Dispose()
. 它只是试图减轻如果消费者不调用
Dispose()
会发生的情况的备份。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.