Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have a single instance for an application, the object will be destroyed when the application closes or crashes. You could call Dispose in the finalizer, but that's not guaranteed to succeed.</p> <p>Unless Dispose does something vital like saving your document (it shouldn't), I'd say, don't worry too much. The fact it is a singleton means it lives forever and disposing is only meant to free resources that would otherwise remain around. The object remains around, so nothing to free.</p> <p>However, some people may frown on this and say "what if the Dispose does something that is important to me, other than freeing resources?". You can still call Dispose after your application has run:</p> <pre><code>static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new LogAnalizzer()); ListService.Dispose(); // or whatever you're going to call it } </code></pre> <hr> <blockquote> <p>I have seen that this is good practice for web applications. I was wondering if this is the same for winform apps too?</p> </blockquote> <p>As a rule of thumb, when your application as a whole needs only one instance of something, a (thread safe) singleton is often a good idea: a cache, connection to a database, a proxy, the application itself, a logger. There's nothing that says that singletons shouldn't apply to your WinForm apps also.</p> <p>Remember however that you must carefully think about your design. What if you have a logger and the file becomes inaccessible? What if a proxy looses connection? etc, etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload