dispose

Windows Forms: Is there a way to wait for all pending Invokes to a Control to end?

不想你离开。 提交于 2020-01-16 00:59:33
问题 I need to do this in order to solve a deadlock. My Windows Forms Control has a reference to a C++/CLI class which wraps a C++ native class. The native class makes callbacks to the C++/CLI class, which maps them to events handled by the form. These callbacks are called from a thread which runs all the time. When I want to dispose the control, I unregister all events, so that the native class can't call back anymore. Once that's done, I dispose the C++/CLI wrapper, which in turn destroys the

Why do threads waiting on a ManualResetEvent continue waiting even when Close() is called?

雨燕双飞 提交于 2020-01-12 18:57:08
问题 We were surprised to learn today that threads waiting on a ManualResetEvent continue waiting on the event even when it's closed. We would have expected that calling Close() would implicitly signal the waiting threads. We tracked this down as a reason some of our windows services were not shutting down as fast as we'd like. We're changing all of our Dispose implementations that close ManualResetEvent references to call Set first. Can anyone explain why Close doesn't implicitly call Set ? When

Should I bother calling dispose on objects which share lifetime of process?

时光总嘲笑我的痴心妄想 提交于 2020-01-12 16:00:36
问题 I am aware that all objects which implement IDisposable should be disposed of as soon as they are no longer needed in order to free the memory used by their unmanaged resources. My question relates to objects which I know for a fact will live until the host process itself is terminated. Would it make any difference if I dispose of them or not? Is there any chance of memory not being freed when the process dies? What about GDI objects? Would the GDI handles be freed when the process dies even

Should I bother calling dispose on objects which share lifetime of process?

点点圈 提交于 2020-01-12 15:58:09
问题 I am aware that all objects which implement IDisposable should be disposed of as soon as they are no longer needed in order to free the memory used by their unmanaged resources. My question relates to objects which I know for a fact will live until the host process itself is terminated. Would it make any difference if I dispose of them or not? Is there any chance of memory not being freed when the process dies? What about GDI objects? Would the GDI handles be freed when the process dies even

Why Dispose is not called even with using-statement?

不羁的心 提交于 2020-01-11 10:53:06
问题 I have this console application (.NET Framework 4.5.2): class Program { static void Main(string[] args) { using (var result = new Result()) { result.Test(); } } } public class Result : IDisposable { public void Test() { int a = 1; int b = 1 / (a - 1); } public void Dispose() { Console.WriteLine("Dispose"); } } Why Dispose method is not called? A breakpoint is not hit in Dispose after the DivideByZero -exception and there is no output on the console (because the app exits). 回答1: As per MS Docs

Dispose object that has been instantiated as method parameter c#

£可爱£侵袭症+ 提交于 2020-01-03 14:05:10
问题 I have the following classes: private static readonly string ConnectionString = "Dummy"; public static SqlConnection GetConnection() { SqlConnection Connection = new SqlConnection(ConnectionString); return Connection; } public static SqlDataAdapter GetDataAdapter(string Query) { SqlDataAdapter Adapt = new SqlDataAdapter(Query, GetConnection()); return Adapt; } How do I dispose the SqlConnection object that is instantiated when GetConnection() is passed as parameter in my SqlDataAdapter

Is it OK to have a virtual Dispose() method as long as all classes in the hierarchy only use managed resources? [duplicate]

自古美人都是妖i 提交于 2020-01-03 13:10:13
问题 This question already has answers here : Minimal IDisposable implimenation for managed resources only (3 answers) Closed 4 years ago . I know that the general guideline for implementing the dispose pattern warns against implementing a virtual Dispose() . However, most often we're only working with managed resources inside a class and so the full dispose pattern seems like an overkill - i.e. we don't need a finalizer. In such cases, is it OK to have a virtual Dispose() in the base class?

Disposing of SQL Connection

ぃ、小莉子 提交于 2020-01-02 07:15:09
问题 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

Why do we need Dispose() method on some object? Why doesn't the garbage collector do this work?

徘徊边缘 提交于 2020-01-02 01:07:06
问题 The question is: why do we need to call Dispose() on some objects? Why doesn't the garbage collector collect the object when it goes out of scope? I am trying to understand the reason why it was implemented like that. I mean, wouldn't it be easier if Dispose() was called when the garbage collector collected out of scope objects. 回答1: The garbage collector is non-deterministic - it collects objects at some point after they're no longer referenced, but it's not guaranteed to happen in a timely

Why do we need Dispose() method on some object? Why doesn't the garbage collector do this work?

荒凉一梦 提交于 2020-01-02 01:06:11
问题 The question is: why do we need to call Dispose() on some objects? Why doesn't the garbage collector collect the object when it goes out of scope? I am trying to understand the reason why it was implemented like that. I mean, wouldn't it be easier if Dispose() was called when the garbage collector collected out of scope objects. 回答1: The garbage collector is non-deterministic - it collects objects at some point after they're no longer referenced, but it's not guaranteed to happen in a timely