dispose

Multithreading exception and Dispose. Why Dispose didn't call?

陌路散爱 提交于 2019-12-04 15:30:39
'using' statement guaranteed that for the object will be called Dispose method. In this example this is not happening. And finalizer method didn't call too. Why all this? And how I can change code for guaranteed disposing of my objects when exceptions on other threads can happens? class Program { static void Main(string[] args) { Thread th1 = new Thread(ThreadOne); Thread th2 = new Thread(ThreadTwo); th1.Start(); th2.Start(); th1.Join(); th2.Join(); } static void ThreadOne() { using (LockedFolder lf = new LockedFolder(@"C:\SomeFodler")) { // some pay load Thread.Sleep(5000); } } static void

Interesting event “Dispose” behaviour

大兔子大兔子 提交于 2019-12-04 12:25:42
I have noticed interesting behaviour in our .NET WinForms app. We have an mdi form that has many mdi children added. These child forms listen to a "broadcast" event which is in essence a call to refresh itsself. The event is declared in a base class and the listening events added in the child forms. I've noticed that even when these child forms are closed, the events are still being hit, if the event is not explicitly removed in the Dispose() method. What is the reasoning behind this? Surely if the form is closed, the events should be detached/disposed of? Is it because the actual event itself

Thread-Safety of Dispose methods?

巧了我就是萌 提交于 2019-12-04 11:30:23
问题 MSDN documents the thread-safety of instances members of BCL types pretty well, but I have never really seen information indicating how the Dispose method of IDisposable types can be called. Is the Dispose method a) guaranteed to be thread-safe for all classes, b) never guaranteed to be thread-safe, c) guaranteed to be thread-safe for some classes (if so, where is this specifically documented)? Finally, if the Dispose method is guaranteed to be thread-safe, does that mean I have to put a lock

WinForms and disposing custom controls

≯℡__Kan透↙ 提交于 2019-12-04 09:22:56
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? You should add the ImageList to your control's Components collection, then the base-class implementation of Dispose will Dispose everything in that collection, and you won't have to

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

[亡魂溺海] 提交于 2019-12-04 09:19:37
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? If your resources are created well, this shouldn't matter (much). However, many poorly created libraries don't do proper checking. Disposing of resources in reverse of their

Entity Framework - How should I instance my “Entities” object

走远了吗. 提交于 2019-12-04 07:58:25
问题 I'm a total newbie at Entity Framework and ASP.Net MVC, having learned mostly from tutorials, without having a deep understanding of either. (I do have experience on .Net 2.0, ADO.Net and WebForms) My current doubt comes from the way I'm instancing my Entities objects. Basically I'm doing this in my controllers: public class PostsController : Controller { private NorthWindEntities db = new NorthWindEntities(); public ActionResult Index() { // Use the db object here, never explicitly Close

Does GC collects garbage from Metaspace?

▼魔方 西西 提交于 2019-12-04 05:22:16
Always I thought that garbage collector clear only heap and now I think so. In java 8 permGen was deleted and it was replaced by Metaspace. And As I understood Metaspace is garbage collected( https://stackoverflow.com/a/24075360/2674303 ) Who collects garbage from Metaspace? I think your confusion stems from the colloquial term “garbage collection” which is widely used but doesn’t really describe what happens in a managed environment. Memory Management is a complex process which is, simplified, about: Identifying the objects which are garbage, which is actually a process of determining which

Dispose a JFrame on button click from another JFrame

笑着哭i 提交于 2019-12-04 05:16:10
问题 As many probably know, I am a complete Java newbie. I have already attempted to research this (by reading other posts on StackOverflow, Googling and asking a friend who is less of a java newbie) but I can't figure it out. The answer is probably clear and easy but I am blind to it. I am attempting to dispose a JFrame from a different frame. My application is supposed to work as follows: Frame X has a button, when pressed: spawns frame Y Frame Y has a button, when pressed: spawns frame Z Frame

Is closing/disposing an SqlDataReader needed if you are already closing the SqlConnection?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 03:37:19
问题 I noticed This question, but my question is a bit more specific. Is there any advantage to using using (SqlConnection conn = new SqlConnection(conStr)) { using (SqlCommand command = new SqlCommand()) { // dostuff } } instead of using (SqlConnection conn = new SqlConnection(conStr)) { SqlCommand command = new SqlCommand(); // dostuff } Obviously it does matter if you plan to run more than one command with the same connection, since closing an SqlDataReader is more efficient than closing and

Do I need to call Graphics.Dispose()?

与世无争的帅哥 提交于 2019-12-04 03:28:35
问题 In a VB.NET program I'm creating a new bitmap image, I then call Graphics.FromImage to get a Graphics object to draw on the bitmap. The image is then displayed to the user. All the code samples I've seen always call .Dispose() on Bitmaps and Graphics objects, but is there any need to do that when neither have touched files on disk? Are there any other unmanaged resources that these objects might have grabbed that wouldn't be cleared by the garbage collector? 回答1: Yes. Always call Dispose() on