dispose

WinForms and disposing custom controls

若如初见. 提交于 2019-12-06 06:21:25
问题 I have the following class: public class NewListBox : ListBox { public NewListBox() { } private ImageList _myImageList; public ImageList ImageList { get { return _myImageList; } set { _myImageList = value; } } } I am interested in whether disposing of this object will trigger the disposal of fields on the object, such as the ImageList, or should i implement (override) the Dispose method and do this work myself? 回答1: You should add the ImageList to your control's Components collection, then

Garbage Collector too slow when working with large images

纵然是瞬间 提交于 2019-12-06 05:41:42
I am using Emgu OpenCV to grab images from a webcam and want to visualize them with WPF Image Control. So I need to convert the image from Mat to something compatible with Image control. So I took this class from the Emgu examples: public static class BitmapSourceConvert { /// <summary> /// Delete a GDI object /// </summary> /// <param name="o">The poniter to the GDI object to be deleted</param> /// <returns></returns> [DllImport("gdi32")] private static extern int DeleteObject(IntPtr o); /// <summary> /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of

Dispose method in web api 2 web service

早过忘川 提交于 2019-12-06 04:02:56
问题 I am coding an MVC 5 internet application with a web api 2 web service. Do I need a dispose method for the DbContext class in a web service? It is not there as default. 回答1: Actually, System.Web.Http.ApiController already implements IDisposable : // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // ... public abstract class ApiController : IHttpController, IDisposable { // ... #region IDisposable public void

C#: Is there an Advantage to Disposing Resources in Reverse Order of their Allocation?

天大地大妈咪最大 提交于 2019-12-06 03:36:14
问题 Many years ago, I was admonished to, whenever possible, release resources in reverse order to how they were allocated. That is: block1 = malloc( ... ); block2 = malloc( ... ); ... do stuff ... free( block2 ); free( block1 ); I imagine on a 640K MS-DOS machine, this could minimize heap fragmentation. Is there any practical advantage to doing this in a C# /.NET application, or is this a habit that has outlived its relevance? 回答1: If your resources are created well, this shouldn't matter (much).

Memory release with IDisposable and without IDisposable

心已入冬 提交于 2019-12-06 01:50:19
In my app I have a large object that's created every few seconds. I do with it some job and then I don't need it anymore. I saw in the task manager that the ram size goes up even if I don't have any reference to the object and it needs to be collected. After implementing IDisposable the ram goes down immediately. Why is this? I didn't do GC.Collect , I just released the object and told the GC it doesn't need to call the finalizer for my object. EDIT: Here is the code I use for my IDisposable objects: public void Dispose() { base.Dispose(); Dispose(true); GC.SuppressFinalize(); } private void

Returning value from JDialog; dispose(), setVisible(false) - example

旧城冷巷雨未停 提交于 2019-12-06 00:02:14
问题 I know, that this question appears quite frequently in SO like here: but I would like to present some very specific example... I'm simply not sure if I make things right. I've got a JDialog in which I can type some values, select some checkboxes... whatever... I've got also some Response object created in MyDialog which represents the MyDialog's "answer". In JFrame which calls/creates JDialog: MyDialog d = new MyDialog(this, ...); d.showDialog(); // After MyDialog is closed (it's modal):

Disposing of SQL Connection

本秂侑毒 提交于 2019-12-05 19:32:16
I have a SQL class that connects to the DB and retreives a DataTable. I am aware that the SqlConnection must be disposed when finished. I know this can be done using a using block, but is it also acceptable to put the Dispose() call inside the destructor of this class? Herre is my code: public class SQLEng { //Connection String Property //Must be set to establish a connection to the database public string ConnectionString{ get; set; } SqlConnection _Conn; //Overridden Constructor enforcing the Connection string to be set when created public SQLEng(string connectionString) { ConnectionString =

Custom dialog using JOptionPane API won't dispose

帅比萌擦擦* 提交于 2019-12-05 18:55:07
I've been just playing with JOptionPane API to show a custom dialog and I've found a strange situation: when I choose either OK or Cancel option or press Esc key this dialog won't dispose as expected. The thing is, instead of using this single line to display a modal dialog: JOptionPane.showConfirmDialog( null , "The quick brown fox jumps over the lazy dog." , "New Dialog" , JOptionPane.OK_CANCEL_OPTION , JOptionPane.PLAIN_MESSAGE); I wanted to use the API, setting all the parameters one by one and displaying a dialog as shown in the docs (see Direct Use section): JOptionPane pane = new

Proper disposal of WebSocket in a WebSocketContext

拟墨画扇 提交于 2019-12-05 18:30:19
In ASP.NET, when the handler you provide to HttpContext.AcceptWebSocketRequest gets a AspNetWebSocketContext , should you dispose the context's WebSocket when you are done with it? Or does the web socket get disposed automatically, perhaps after you call WebSocket.CloseAsync ? You should not dispose the web socket. In fact you cannot. AspNetWebSocket.Dispose always throws a NotSupportedException . The summary and exception sections of the MSDN documentation are incorrect. Fortunately, the remarks section is helpful: ASP.NET automatically calls the Dispose method on a AspNetWebSocket object to

return the variable used for using inside the using C#

孤者浪人 提交于 2019-12-05 16:57:21
问题 I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; } } Will this Dispose the properties variable?? After doing this am still getting this Warning: Warning 34 CA2000 : Microsoft.Reliability : In method 'test.test', call System.IDisposable.Dispose on object 'properties' before all references to it are out of scope. Any Ideas? Thanks