dispose

Should I always disconnect event handlers in the Dispose method?

江枫思渺然 提交于 2019-11-26 11:19:42
问题 I\'m working in C# and my workplace has some code standards. One of them is that each event handler we connect (such as KeyDown ) must be disconnected in the Dispose method. Is there any good reason for that? 回答1: Unless you expect the publisher of the event to outlive the subscriber, there's no reason to remove the event handler, no. This is one of those topics where folk lore has grown up. You really just need to think about it in normal terms: the publisher (e.g. the button) has a

Difference between destructor, dispose and finalize method

自作多情 提交于 2019-11-26 10:18:39
问题 I am studying how garbage collector works in c#. I am confused over the use of Destructor , Dispose and Finalize methods. As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class. The Dispose method is meant to provide the user to control the garbage collection. The Finalize method frees the

Clear controls does not dispose them - what is the risk?

喜夏-厌秋 提交于 2019-11-26 09:57:33
问题 There are multiple threads(a, b, c etc.) about the fact that Clear () ing items in the .NET component containers does not Dispose them(by calling Dispose( true ). Most frequently, IMHO, the Clear-ed components are not used anymore in the application, so it needs explicitly be Disposed after Clearing them from the parent containers. Maybe is a good idea that collection\'s Clear method had a bool parameter dispose that when in true also disposes the collection elements before its removing from

Right way to dispose Image/Bitmap and PictureBox

喜夏-厌秋 提交于 2019-11-26 09:01:56
问题 I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls or whatever I want. There are two things I am doing. Drawing custom shapes and loading bitmaps from .png files. The next line locks the file when loading (which is an undesired scenario): Bitmap bmp = new Bitmap(\"file.png\"); So I am using another way to load bitmap. public static Bitmap LoadBitmap(string path) { using

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

妖精的绣舞 提交于 2019-11-26 07:43:20
问题 I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[\"MyConn\"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = \"...\"; connection.Open(); command.ExecuteNonQuery(); } Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand ? 回答1: Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn

Does garbage collector call Dispose()? [duplicate]

守給你的承諾、 提交于 2019-11-26 07:38:29
问题 This question already has an answer here: Will the Garbage Collector call IDisposable.Dispose for me? 9 answers I thought the GC would call Dispose eventually if your program did not but that you should call Dispose() in your program just to make the cleanup deterministic. However, from my little test program, I don\'t see Dispose getting called at all.... public class Test : IDisposable { static void Main(string[] args) { Test s = new Test(); s = null; GC.Collect(); Console.ReadLine(); }

Does SqlCommand.Dispose close the connection?

天大地大妈咪最大 提交于 2019-11-26 07:28:57
问题 Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(\"GetSomething\", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } My concern is : Will the Dispose method of the SqlCommand (which is called when exiting the using block) close the underlying SqlConnection object or not? 回答1: No, Disposing of the SqlCommand will not effect the Connection. A better approach

What happens if I don't call Dispose on the pen object?

雨燕双飞 提交于 2019-11-26 05:33:00
问题 What happens if I don\'t call Dispose on the pen object in this code snippet? private void panel_Paint(object sender, PaintEventArgs e) { var pen = Pen(Color.White, 1); //Do some drawing } 回答1: The Pen will be collected by the GC at some indeterminate point in the future, whether or not you call Dispose . However, any unmanaged resources held by the pen (e.g., a GDI+ handle) will not be cleaned up by the GC. The GC only cleans up managed resources. Calling Pen.Dispose allows you to ensure

What is the promise disposer pattern?

纵饮孤独 提交于 2019-11-26 04:48:57
I've read about the promise disposer pattern in several places but I can't figure out what it is. It was suggested to me to use it in code that looks like: function getDb(){ return myDbDriver.getConnection(); } var users = getDb().then(function(conn){ return conn.query("SELECT name FROM users").finally(function(users){ conn.release(); }); }); What's the promise disposer pattern and how does it apply here? Note - in native promises, I shim .finally as "add both rejection and fulfillment handlers that return the value but perform an action". I'm using bluebird in this case if it matters.

Disposing WPF User Controls

狂风中的少年 提交于 2019-11-26 04:39:51
问题 I have created a custom WPF user control which is intended to be used by a third party. My control has a private member which is disposable, and I would like to ensure that its dispose method will always get called once the containing window/application is closed. However, UserControl is not disposable. I tried implementing the IDisposable interface and subscribing to the Unloaded event but neither get called when the host application closes. If at all possible, I don\'t want to rely on