dispose

Try/Finally block vs calling dispose?

我的梦境 提交于 2019-11-29 09:23:41
问题 Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new Streamwriter(...)) { writer.blahblah } I mean in the second example you really should be putting it in a try block anyway so adding the finally block really doesn't use much more effort. I get that the entire thing might be encompassed in a bigger try block but

How to dispose objects having asynchronous methods called?

。_饼干妹妹 提交于 2019-11-29 09:19:23
I have this object PreloadClient which implements IDisposable , I want to dispose it, but after the asynchronous methods finish their call... which is not happening private void Preload(SlideHandler slide) { using(PreloadClient client = new PreloadClient()) { client.PreloadCompleted += client_PreloadCompleted; client.Preload(slide); } // Here client is disposed immediately } private void client_PreloadCompleted(object sender, SlidePreloadCompletedEventArgs e) { // this is method is called after a while, // but errors are thrown when trying to access object state (fields, properties) } So, any

Should I manually dispose the socket after closing it?

£可爱£侵袭症+ 提交于 2019-11-29 09:03:48
Should I still call Dispose() on my socket after closing it? For example: mySocket.Shutdown(SocketShutdown.Both); mySocket.Close(); mySocket.Dispose(); // Redundant? I was wondering because the MSDN documentation says the following: Closes the Socket connection and releases all associated resources. Calling Close internally calls Dispose so you don't need to call both. From .NET Reflector : public void Close() { if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "Close", (string) null); } ((IDisposable)this).Dispose(); if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this,

unable to delete image after opening it in vb.net app

自古美人都是妖i 提交于 2019-11-29 07:14:28
I have this code: Dim xx as image xx = image.fromfile(Fileloc) picturebox.image = xx And i can't delete the file even though I've loaded it into a picture box. If I add this line: xx.dispose the picture box becomes a big red X. I only want to delete the images when my application is closing (they are temp files). So shall I just dispose them before I delete them? Don't use Image.FromFile , it keeps the file open. From MSDN : The file remains locked until the Image is disposed. Do that instead : Dim xx as Image Using str As Stream = File.OpenRead(Fileloc) xx = Image.FromStream(str) End Using

Why call Dispose() before main() exits?

眉间皱痕 提交于 2019-11-29 06:14:38
My .net service cleans up all its unmanaged resources by calling resourceName.Dispose() in a finally block before the Main() loop exits. Do I really have to do this? Am I correct in thinking that I can’t leak any resources because the process is ending? Windows will close any handles that are no longer being used, right? There is no limit to the types of resources that may be encapsulated by an object implementing IDisposable . The vast majority of resources encapsulated by IDisposable objects will be cleaned up by the operating system when a process shuts down, but some programs may use

Is there a list of common object that implement IDisposable for the using statement?

六眼飞鱼酱① 提交于 2019-11-29 05:43:22
I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection , MemoryStream , etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket. Anything like that exist? If not, maybe we should make one. Microsoft FxCop has a rule checking that you use an IDisposbale in a using block. Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/ . Not sure if that's what you

How do I prevent a form object from disposing on close?

烈酒焚心 提交于 2019-11-29 05:40:40
I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing? By default, when you close a form, it will be disposed. You have to override the Closing event to prevent it, for example: // Use this event handler for the FormClosing event. private void MyForm_FormClosing(object sender, FormClosingEventArgs e) { this.Hide(); e.Cancel = true; // this cancels the close event. } You can cancel the close event and hide instead. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e

Does “using” statement always dispose the object?

那年仲夏 提交于 2019-11-29 05:29:27
Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { // ... throw new UnexplainedAndAnnoyingException(); } Yes, that's the whole point. It compiles down to: SomeDisposableType obj = new SomeDisposableType(); try { // use obj } finally { if (obj != null) ((IDisposable)obj).Dispose(); } Be careful about your terminology here; the object itself is not deallocated. The Dispose() method is called and, typically, unmanaged

Does closing a database connection in Dispose method is right?

坚强是说给别人听的谎言 提交于 2019-11-29 04:50:28
I've had a suspicion that a database connection used in one of our applications is not always closed. I went to see the code and I've found a class DataProvider that has SqlConnection object. The connection is opened in the constructor of this class and closed in it's Dispose method (don't judge that, I know keeping an open connection is evil, it's just not my code and it's not the point of the question anyway). The Dispose method is implemented like this: protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { if (_conn != null) _conn.Close(); } _disposed = true; }

How to dispose managed resource in Dispose() method in C#?

会有一股神秘感。 提交于 2019-11-29 03:49:50
I know Dispose() is intended for unmanaged resource, and the resource should be disposed when it is no longer needed without waiting for the garbage collector to finalize the object. However, when disposing the object, it suppress finalization of the garbage collector (GC.SuppressFinalize(this); in the code below). This means that if the object includes managed resource, we will have to take care of that too because garbage collector will not clean this up. In the example code below (from MSDN), "Component" is a managed resource, and we call dispose() for this resource (component.Dispose()).