dispose

Event Handler Stops Working After Dispose

江枫思渺然 提交于 2019-12-13 05:24:25
问题 The three ImageButtons below are added to a panel (pBreakfast) in a page's codebehind: ImageButton ibSR = new ImageButton(); ibSR.ID = "ibStickyRoll"; ibSR.ImageUrl = "/images/Breakfast.gif"; ibSR.Click += new ImageClickEventHandler(ImageButton_Click); pBreakfast.Controls.Add(ibSR); ImageButton ibD = new ImageButton(); ibD.ID = "ibDoughnut"; ibD.ImageUrl = "/images/Breakfast.gif"; ibD.Click += new ImageClickEventHandler(ImageButton_Click); pBreakfast.Controls.Add(ibD); ibD.Dispose(); using

Close application from captive IE session

女生的网名这么多〃 提交于 2019-12-13 03:26:24
问题 My question is: "Can this be done better?" and if so, how? Any ideas? We need to start a captive IE session from within an "invisible" C# .NET 3.5 application, and quit both the IE session and the "parent" application after processing a certain request. I've been mucking around with this problem for the last week or so... and this morning I've finally reached what I think is a robust solution; but I'm a bit of a C# noob (though I've been a professional programmer for 10 years), so I'm seeking

How to dispose all children of Composite object?

不羁的心 提交于 2019-12-13 02:57:29
问题 I hava a Composite descComp with some stuff in it... basically it is a container for a form, consisting of number of labels, Combos and buttons, all aligned in a line. My form is not finite, I have a button that adds one extra line for extra input. However for that to work it seams I have to dispose old children of my descComp... private void populateConstantMain(ContentData tariffConstantsOfType, Composite descComp,GridLayout descCompLayout, Boolean resize) { int arraySize; if (resize ==

Does `Control.Dispose()` remove all event registrations?

≡放荡痞女 提交于 2019-12-13 02:53:24
问题 When I call Dispose() on a System.Windows.Forms.Control , does it automatically remove all event registrations? So for example, is the following sufficient: var button = new Button(); button.Click += someObject.clickHandler; // ... button.Dispose(); Or do I need to unregister the event handler explicitly, like this: var button = new Button(); button.Click += someObject.clickHandler; // ... button.Click -= someObject.clickHandler; button.Dispose(); 回答1: Nope, It doesn't. You ought to remove

Implementing IDisposable

Deadly 提交于 2019-12-13 02:38:08
问题 VS generates automatically these regionized procedures when I Implement IDisposable: #Region "IDisposable Support" Private disposedValue As Boolean ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(disposing As Boolean) If Not Me.disposedValue Then If disposing Then ' TODO: dispose managed state (managed objects). End If ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. ' TODO: set large fields to null. End If Me.disposedValue = True

StreamWriter inside Dispose

早过忘川 提交于 2019-12-13 00:31:41
问题 I have the following code: public void Dispose() { bool append = true; using(var log = new System.IO.StreamWriter("log.txt", append)) { log.WriteLine("Disposing"); log.Flush(); } } So there's the risk that the StreamWriter may throw an exception, would that then mean that my object would not get disposed? Would simply wrapping this in a Try/Catch solve the issue? Thanks. 回答1: It depends on what you mean by this: would that then mean that my object would not get disposed? Your object has had

System.Drawing objects unmanaged resource disposing

偶尔善良 提交于 2019-12-12 23:15:52
问题 I have the following code: using System.Drawing; ... Graphics _graphics = Graphics.FromImage(...) ... public void Draw(Stream stream, Rectangle rect, Color color) { _graphics.FillRectangle(new SolidBrush(Color), 0, 0, Width, Height); _graphics.DrawImage(new Bitmap(stream), rect); } Should I surround the drawImage with using on new Bitmap(...) ? Same question on the new SolidBrush(...) 回答1: Yes, you should wrap them in using statements. Also you should ensure that the Dispose methods is

C# class: Do logging / housekeeping, should I use a destructor?

为君一笑 提交于 2019-12-12 18:01:16
问题 I have a c# class. Whenever this class is not in use anymore I want to do some things. For example log the current state and so on. I want to be sure that this method is run everytime when the class is not used anymore. I don't want just use a simple method because I can't be sure that every user is calling it. I have no resources (like file handles) to clear up. Is the best way to use a destructor? "not in use" is when (for example): a user uses my class in a form and the form is closed the

Calling dispose() for Font in Windows form

偶尔善良 提交于 2019-12-12 17:03:27
问题 I've been running into the same issue as described in this question. That is, Fortify complained about the creation of font objects, for which the new Font() statements are generated code in the designer.cs files. When looking at profiling results I do get the impreesion I need to do something about this, at least in some cases. This is, of course, an issue only if the develper has assigned a font of his choice to the form in the graphical designer. My plan is to undo that choice and to

Should I dispose of the old font when changing the font of a control?

浪尽此生 提交于 2019-12-12 16:15:24
问题 C#, Windows Forms app. We're re-skinning our application and I'm also changing the awful default font we used in the old one. So I thought I'd call the following function to change the font of all controls on a form when the form is loaded. internal static void SetFonts(Control control) { Font oldFont = control.Font; if (oldFont.Name != GlobalFontName) { string familyName = GlobalFontName; Font newFont = new System.Drawing.Font(familyName, oldFont.Size, oldFont.Style, GraphicsUnit.Point, 0);