dispose

Do I need to unsubscribe events in my Form?

橙三吉。 提交于 2019-12-18 09:15:27
问题 I'm trying to understand how a Control events are unsubscribed. Suppose I have a textbox and I have subscribed the TextChanged event using the WinForms designer. Is the TextChanged event automatically unsubscribed in the Textbox destructor, or must I explicitly unsunscribe to avoid memory leaks? public void InitializeComponents() { ... this.emailTextBox.TextChanged += emailTextBox_TextChanged; ... } public override void Dispose() { if( disposing ) { // DO I REALLY NEED THIS LINE? this

Why does Code Analysis tell me, “Do not dispose objects multiple times” here:

[亡魂溺海] 提交于 2019-12-18 09:09:22
问题 On this code: public static string Base64FromFileName(string fileName) { try { FileInfo fInfo = new FileInfo(fileName); long numBytes = fInfo.Length; FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); byte[] bdata = br.ReadBytes((int)numBytes); br.Close(); fStream.Close(); return Convert.ToBase64String(bdata); } catch(Exception e) { throw e; } } ...I get, courtesy of Visual Studio's Code Analysis tool, the warning, " Do

Windows Form Fonts Questions Part 1

牧云@^-^@ 提交于 2019-12-18 06:48:25
问题 I have some user controls in a windows form. I wonder if i set the Font property of the main form, will its child get a) a copy of the new Font , or b) a reference to the new Font , or c) nothing? Does a font need to be disposed? For example, can I do the following code safely? form.Font = new Font(...); Will a font get disposed automatically when the parent ( Form or UserControl ) is disposed? Thanks, Gilbert 回答1: Both. The Font property is its own .NET object. Winforms however caches the

How to dispose objects having asynchronous methods called?

梦想与她 提交于 2019-12-18 05:43:32
问题 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

How to dispose objects having asynchronous methods called?

跟風遠走 提交于 2019-12-18 05:41:29
问题 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

Should I manually dispose the socket after closing it?

北战南征 提交于 2019-12-18 05:27:09
问题 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. 回答1: 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

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

╄→гoц情女王★ 提交于 2019-12-18 04:33:29
问题 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. 回答1: Microsoft FxCop has a rule checking that you use an IDisposbale in a using block. 回答2: Perhaps

Types that own disposable fields should be disposable. how to solve this warning?

我的未来我决定 提交于 2019-12-18 03:41:37
问题 I tried using Run Code Analysis option in VisualStudio 2012 , as a result of it I got a warning as CA1001 Types that own disposable fields should be disposable Implement IDisposable on 'DBConnectivity' because it creates members of the following IDisposable types: 'SqlConnection', 'SqlCommand'. I referred some question in SO, but I couldn't catch the point regarding IDisposable and following is the class, responsible for this warning. class DBConnectivity { public SqlConnection connection =

Need I remove controls after disposing them?

不想你离开。 提交于 2019-12-18 03:11:44
问题 .NET 2 // dynamic textbox adding myTextBox = new TextBox(); this.Controls.Add(myTextBox); // ... some code, finally // dynamic textbox removing myTextBox.Dispose(); // this.Controls.Remove(myTextBox); ?? is this needed Little explanation Surely, if I Dispose a control I will not see it anymore, but anyway, will remain a "Nothing" in the parent controls collection? need I also, like MSDN recommends, remove all handlers from the control? 回答1: No, you don't. I tried it. You can paste the

How does one tell if an IDisposable object reference is disposed?

Deadly 提交于 2019-12-18 03:00:48
问题 Is there a method, or some other light-weight way, to check if a reference is to a disposed object? P.S. - This is just a curiousity (sleep well, not in production code). Yes, I know I can catch the ObjectDisposedException upon trying to access a member of the object. 回答1: No - default implementation of IDisposable pattern does not support it 回答2: System.Windows.Forms.Control has an IsDisposed property which is set to true after Dispose() is called. In your own IDisposable objects, you can