dispose

Async Disposable.Create

老子叫甜甜 提交于 2019-12-07 05:54:00
问题 Disposable.Create require an Action as parameter. The Action is run when the Rx subscription is being disposed. When disposing a Rx subscription I’d like to run some asynchronous clean up code, however using async () => with Action is identical to async void , which I’d like to avoid. For more details on why I want to avoid this, see here. Is it possible to create something like a Disposable.AsyncCreate , which accepts Func<Task> rather than Action . If so how should I use it as part of a

.NET - Why is disposing of standard output only allowed during unit tests?

早过忘川 提交于 2019-12-07 03:54:25
First of all, I want to point out that I do not really want to dispose of the standard output...I just want to know why I'm seeing the described behavior. I didn't write the bad code described. I'm using .NET 4 for the unit tests and .NET 3.5 for the code being tested. I'm using MSTest for .NET 4 as my testing framework. Recently I have been working with a library that was throwing errors due to the blunder of disposing of the standard error output. (See LibTiff.NET ReadDirectory is giving System.ObjectDisposedException Only During Unit Tests ). This is relatively what their code looked like:

Does OledbConnection.Dispose() close the connection? [duplicate]

隐身守侯 提交于 2019-12-07 02:32:25
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is there any need to close a DbConnection if a using clause is used? Does OledbConnection.Dispose() close the connection? I am aware that SqlConnection does but what about OledbConnection ? 回答1: Yes, it also does. Source: OleDbConnection.Dispose Method (Boolean) The Dispose method calls Close, and removes the OleDbConnection from the connection pool. For detailed information see the Remarks section on the

Throwing exception in finalizer to enforce Dispose calls:

吃可爱长大的小学妹 提交于 2019-12-07 02:17:04
问题 Here is the typical IDisposable implementation that I believe is recommended: ~SomeClass() { Dispose(false); } public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } protected virtual void Dispose(bool isDisposing) { if(isDisposing) { // Dispose managed resources that implement IDisposable. } // Release unmanaged resources. } Now, to my understanding, the idea behind the finalizer there is that if I don't call Dispose, my unmanaged resources will be released properly still.

Class Destructor Problem

时光总嘲笑我的痴心妄想 提交于 2019-12-07 00:23:17
问题 I am making a simple class that contains a StreamWrite class Logger { private StreamWriter sw; private DateTime LastTime; public Logger(string filename) { LastTime = DateTime.Now; sw = new StreamWriter(filename); } public void Write(string s) { sw.WriteLine((DateTime.Now-LastTime).Ticks/10000+":"+ s); LastTime = DateTime.Now; } public void Flush() { sw.Flush(); } ~Logger() { sw.Close();//Raises Exception! } } But when I close this StreamWriter in the destructor, it raises an exception that

I want my memory back! How can I truly dispose a control?

孤街醉人 提交于 2019-12-07 00:22:57
问题 I have an application I am making that creates a large number of windows controls (buttons and labels etc). They are all being made dynamically through functions. The problem I'm having is, when I remove the controls and dispose them, they are not removed from memory. void loadALoadOfStuff() { while(tabControlToClear.Controls.Count > 0) tabControlToClear.Controls[0].Dispose(); //I even put in: GC.Collect(); GC.WaitForPendingFinalizers(); foreach(String pagename in globalList)

C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader

一笑奈何 提交于 2019-12-06 23:49:15
问题 I have lots of code like this: FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open); using (XmlTextReader reader = new XmlTextReader(fs)) { /* Some other code */ } This gives me the following Code Analysis warning: CA2000 : Microsoft.Reliability : In method 'SF_Tester.Run()', object 'fs' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'fs' before all references to it are out of scope. If I follow the suggestion and I put the File.Open in a

How to dispose a Writeable Bitmap? (WPF)

爷,独闯天下 提交于 2019-12-06 23:00:23
问题 Some time ago i posted a question related to a WriteableBitmap memory leak, and though I received wonderful tips related to the problem, I still think there is a serious bug / (mistake made by me) / (Confusion) / (some other thing) here. So, here is my problem again: Suppose we have a WPF application with an Image and a button. The image's source is a really big bitmap (3600 * 4800 px), when it's shown at runtime the applicaton consumes ~90 MB. Now suppose i wish to instantiate a

Is disposing a panel equivalent to disposing its children plus itself?

ぃ、小莉子 提交于 2019-12-06 12:18:47
Within the following class: class MyPanel : Panel { ... protected override void Dispose(bool disposing) { // My code here } } are the following two code samples equivalent? base.Dispose(disposing); vs if (disposing) { List<Control> ctrls = new List<Control>(this.Controls); this.Controls.Clear(); foreach(Control c in ctrls) { c.Dispose(); } } base.Dispose(disposing); If they have a different effect, what would it be? Edit: I ask this because, for whatever reason, doing it the first way freezes my program before it gets to disposing any of its children ( disposing is true, and Controls contains

C# Dispose : when dispose and who dispose it

我的梦境 提交于 2019-12-06 09:38:44
I'm getting in nerve between dispose and finalize. Here is my example code: public class Car:IDisposable { public string name; public Car() { name = "My Car"; } public void Dispose() { Console.WriteLine("This object has been disposed"); } } public static void Main() { Car anotherCar; using (var car = new Car()) { anotherCar = car; Console.WriteLine("Before dispose. Name is: "+anotherCar.name); } Console.WriteLine("After dispose. Name is: "+anotherCar.name); } The result is: Before dispose. Name is My Car This object has been disposed After dispose. Name is My Car My question is : because C#