idisposable

Interfaces using IDisposable

被刻印的时光 ゝ 提交于 2019-12-11 11:05:35
问题 In regards to IDisposable I'm creating interface that I would expect to use system resources most of the time, but not always. Would it be prudent to anticipate the usage include IDisposable on my Interface? For example I have an interface that provides a mean to synchronize to. interface IDateTimeProvider : IDisposable { int LeapSeconds {get;set;} DateTime LocalNow {get;} DateTime UtcNow {get;} DateTime GpsNow {get;} } class NtpTimeProvider : IDateTimeProvider { // Assume client is setup and

closing connections within a using block

自作多情 提交于 2019-12-11 06:56:10
问题 On reading lots of code, I find that a given Connection object, which implements IDisposable , is is manually closed within a using statement. I see this when I see code related to MySQL. It's not needed to be explicitly closed. Why would the developer close it manually? using(cnn) { //code is here cnn.close(); } Is this a good/helpful measure? 回答1: Explicitly closing in a using block is duplication, misleading and redundant so for me, is a bad thing. 回答2: It depends on the connection. Many

How do I prevent memory leak from SolidColorBrush object?

瘦欲@ 提交于 2019-12-11 05:53:12
问题 There's no option to dispose SolidColorBrush. How do I prevent memory leak from SolidColorBrush object? I can't even use 'using' as SolidColorBrush doesn't implement IDisposable Interface. 回答1: Don't create new SolidColorBrush instances. Use the predefined brushes in System.Windows.Media.Brushes . Otherwise, create a single instance, and re-use that. 回答2: Here is my experience. If I use below code there is a huge memory leak while running it couple hundred thousand times. SolidColorBrush

Why use GC.SuppressFinalize() when there is no Finalizer?

本秂侑毒 提交于 2019-12-11 00:47:45
问题 As the question states, I just wanted to know, because I've been asked and I don't have a clue, is there any reason for this whatsoever? 回答1: When a class does not define a Finalizer (destructor), a call to SuppressFinalize() on an instance of that class has no effect. When you see it, it usually is a left-over of the full Disposable implementation. Just remove it or ignore it. 回答2: The reason might be to prevent potential error if someone adds a finalizer later on and forgets to add GC

Why I can not use disposable objects in object members?

荒凉一梦 提交于 2019-12-10 22:38:57
问题 I don't want to add StreamWriter parameter to Write-To-File procedure but when I'm trying to work with disposable StreamWriter there I'm getting: An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. code: let fileLogger = { new IFlog with member i.FLog level format = use file = LogFile() Printf.kfprintf (fun f -> fprintfn file "[%s][%A] " <| level.ToString() <| DateTime.Now ) file (format) so when

Is it safe to dispose the Icon after calling Icon.ToBitmap()?

為{幸葍}努か 提交于 2019-12-10 21:04:52
问题 After calling System.Drawing.Icon.ToBitmap() to create an image, is it safe to dispose the original Icon ? 回答1: Yes. Icon.ToBitmap draws the Icon to a new Bitmap object so it is safe to dispose it afterwards. Edit: Looking at the Icon.ToBitmap() method in Reflector was interesting. I expected it to be a simple Graphics.DrawImage or Graphics.DrawIcon call but it is more involved than that. As long as it is possible the function will do a memory copy of the icon image data instead, but it will

Why does Code Analysis flag me when using the null conditional operator with Dispose()?

前提是你 提交于 2019-12-10 19:22:59
问题 I have a class that implements IDisposable . I changed my Dispose() method from this: public void Dispose() { if (AccountCreateResetEvent != null) { AccountCreateResetEvent.Dispose(); AccountCreateResetEvent = null; } } to this: public void Dispose() { AccountCreateResetEvent?.Dispose(); AccountCreateResetEvent = null; } Now I get the following error when running Code Analysis: 'Adapter' contains field 'Adapter.AccountCreateResetEvent' that is of IDisposable type: 'AutoResetEvent'. Change the

WPF - Does HwndSource have to be disposed?

大城市里の小女人 提交于 2019-12-10 18:28:58
问题 I'm using HwndSource in a WPF window, which is not the main window, in order to hook a window procedure (WndProc) to receive some messages: WinSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); WinSource.AddHook(new HwndSourceHook(WndProc)); HwndSource implements IDisposable . MSDN is not clear about when/should I dispose it. The docs of HwndSource.FromHwnd explains the technique above: You can use this method to return an HwndSource for a window that is not explicitly an

Why dispose an object which will surely get disposed of soon regardless?

让人想犯罪 __ 提交于 2019-12-10 16:13:58
问题 Suppose I have a procedure for e.g. a button click. And I create a Graphics object. Apparently i'm supposed to dispose of it e.g. using(Graphics gr__=this.CreateGraphics()) { } or with calling .Dispose() in the finally of a try-catch-finally. But considering that the procedure is going to end pretty quickly. Suppose I create it local to the procedure (not global, not in a using). But local to the procedure. Then surely like any other variables, it will get disposed of automatically when the

If a generic collection is instantiated to contain iDisposable items, do the items get disposed?

こ雲淡風輕ζ 提交于 2019-12-10 16:13:12
问题 For example: Queue<System.Drawing.SolidBrush> brushQ = new Queue<System.Drawing.SolidBrush>(); ... brushQ.Clear(); If I don't explicitly dequeue each item and dispose of them individually, do the remaining items get disposed when calling Clear()? How about when the queue is garbage collected? Assuming the answer is "no", then what is the best practice? Do you have to always iterate through the queue and dispose each item? That can get ugly, especially if you have to try..finally around each