dispose

Do I need to consider disposing of any IEnumerable<T> I use?

时光毁灭记忆、已成空白 提交于 2019-11-27 11:02:18
问题 It's recently been pointed out to me that various Linq extension methods (such as Where , Select , etc) return an IEnumerable<T> that also happens to be IDisposable . The following evaluates to True new int[2] {0,1}.Select(x => x*2) is IDisposable Do I need to dispose of the results of a Where expression? Whenever I call a method returning IEnumerable<T> , am I (potentially) accepting responsibility for calling dispose when I've finished with it? 回答1: No, you don't need to worry about this.

Why call dispose(false) in the destructor?

霸气de小男生 提交于 2019-11-27 09:10:00
问题 What follows is a typical dispose pattern example: public bool IsDisposed { get; private set; } #region IDisposable Members public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { //perform cleanup here } IsDisposed = true; } } ~MyObject() { Dispose(false); } I understand what dispose does, but what I don't understand is why you would want to call dispose(false) in the destructor? If you look at

Avoid calling Invoke when the control is disposed

谁说我不能喝 提交于 2019-11-27 08:50:17
I have the following code in my worker thread ( ImageListView below is derived from Control ): if (mImageListView != null && mImageListView.IsHandleCreated && !mImageListView.IsDisposed) { if (mImageListView.InvokeRequired) mImageListView.Invoke( new RefreshDelegateInternal(mImageListView.RefreshInternal)); else mImageListView.RefreshInternal(); } However, I get an ObjectDisposedException sometimes with the Invoke method above. It appears that the control can be disposed between the time I check IsDisposed and I call Invoke . How can I avoid that? There are implicit race conditions in your

using statement FileStream and / or StreamReader - Visual Studio 2012 Warnings

别来无恙 提交于 2019-11-27 07:46:08
The new Visual Studio 2012 is complaining about a common code combination I have always used. I know it seems like overkill but I have done the following in my code 'just to be sure'. using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var sr = new StreamReader(fs)) { // Code here } } Visual studio is 'warning' me that I am disposing of fs more than once. So my question is this, would the proper way to write this be: using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var sr = new StreamReader(fs);

Why disposed object doesn't throw exception on using it after disposing?

江枫思渺然 提交于 2019-11-27 07:36:21
问题 Is it legal to call a method on disposed object? If yes, why? In the following demo program, I've a disposable class A (which implements IDisposable interface).As far as I know, if I pass disposable object to using() construct, then Dispose() method gets called automatically at the closing bracket: A a = new A(); using (a) { //... }//<--------- a.Dispose() gets called here! //here the object is supposed to be disposed, //and shouldn't be used, as far as I understand. If that is correct, then

What happens if i return before the end of using statement? Will the dispose be called?

三世轮回 提交于 2019-11-27 06:56:23
I've the following code using(MemoryStream ms = new MemoryStream()) { //code return 0; } The dispose() method is called at the end of using statement braces } right? Since I return before the end of the using statement, will the MemoryStream object be disposed properly? What happens here? Yes, Dispose will be called. It's called as soon as the execution leaves the scope of the using block, regardless of what means it took to leave the block, be it the end of execution of the block, a return statement, or an exception. As @Noldorin correctly points out, using a using block in code gets compiled

Do I need to dispose a web service reference in ASP.NET?

丶灬走出姿态 提交于 2019-11-27 05:35:13
问题 Does the garbage collector clean up web service references or do I need to call dispose on the service reference after I'm finished calling whatever method I call? 回答1: Instead of worrying about disposing your web services, you could keep only a single instance of each web service, using a singleton pattern. Web services are stateless, so they can safely be shared between connections and threads on a web server. Here is an example of a Web Service class you can use to hold references to your

Do I need to dispose of a Task?

谁都会走 提交于 2019-11-27 05:34:01
I am having fun working with System.Threading.Tasks . Many of the code samples I see, however, look something like so: Dim lcTask = Task.Factory.StartNew(Sub() DoSomeWork()) Dim lcTaskLong = Task.Factory.StartNew(Sub() DoSomeWork(), TaskCreationOptions.LongRunning) Task.WaitAll(lcTask, lcTaskLong) That's the extent of the sample. Tasks implement IDisposable , so obviously I'm supposed to dispose of them, but what if I just want to "Fire and Forget"? If I don't dispose, will I leak threads/handles/memory/karma? Am I using tasks "wrong"? (Should just use a delegate and leave tasks alone?) Can I

Dispose vs Dispose(bool)

三世轮回 提交于 2019-11-27 05:19:06
问题 I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called. But now FXCop is telling me lots of stuff about Disposing = false and calling Dispose(false). I don't see a Dispose method that takes a bool. Do I need to make one? If so, why? Why not just have a method that gets called when it is disposing? I saw some code here: http://msdn

How do you prevent IDisposable from spreading to all your classes?

南笙酒味 提交于 2019-11-27 05:01:56
问题 Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } A Bus has a Driver , the Driver has two Shoe s, each Shoe has a Shoelace . All very silly. Add an IDisposable object to Shoelace Later I decide that some operation on the Shoelace could be multi-threaded, so I add an