A generic Singleton form provider for C #

Source: Internet
Author: User

June 19,200 8

An interesting, although generally basic, problem with Windows Forms is how to implement the Singleton pattern on the forms. What do I mean? Well, consider this...

You're writing a Windows Forms Application and you have a bunch of forms, such as a preference window, an about window and so on. you 'd like these forms to be modeless, yet you don't want the user to be able to create more than one of them.

It's a common problem, but there's no built in way to handle it. moreover, I 've yet to see an elegant solution to it. the method explained below, which is as elegant as it's going to get, creates a pseudo-factory class to manage the problem for you. read on to find out more.

If a form were just any old object, you cocould implement the Singleton pattern in the said class:

View plaincopy to clipboardprint?
  1. Class somesingletonclass {
  2. Protected somesingletonclass (){
  3. }
  4. Private Static somesingletonclass minstance = NULL;
  5. Public static somesingletonclass instance {
  6. Get {
  7. If (minstance = NULL) minstance = new somesingletonclass ();
  8. Return minstance;
  9. }
  10. }
  11. }

Of course, this isn' t thread safe-there are much better ways to do this in the general case. see here for a good explanation. in the case of forms, however, we don't need to be thread safe. windows Forms are explicitly un-threadsafe themselves. they shocould (in most cases) only ever be initialized/used from a single thread. this constraint applies here too.

Now, this won't work for Windows Forms. How come? Well, once the Windows form has been closed (with close () or otherwise) It, generally, gets disposed. we all know you can't use a disposed form again. you'll get a crash when trying to access the form again. so, what's the work around?

Basically, we need to handleFormclosedEvent, so that we know the form has been closed. we can then remove the reference to the form, once its been closed, and start all over again next time it's needed. so we 'd end up with something like this:

View plaincopy to clipboardprint?
  1. Class somesingletonform: FORM {
  2. Protected somesingletonform (){
  3. }
  4. Private Static somesingletonform minstance = NULL;
  5. Public static somesingletonform instance {
  6. Get {
  7. If (minstance = NULL)
  8. {
  9. Minstance =New somesingletonclass ();
  10. Minstance. formclosed + =New formeventhandler (remover );
  11. }
  12. /* Cocould call minstance. Show ();*/
  13. Return minstance;
  14. }
  15. }
  16. Static void remover (Object sender, formclosedeventargs E)
  17. {
  18. Minstance. formclosed-=New formclosedeventhandler (remover );
  19. Minstance =NULL;
  20. }
  21. }

Now we're re getting closer. it seems unnecessary, however, to implement this on every single form that we want to be a singleton form: it's a lot of copying identical code, and it's prone to bugs.

So what can we do? We create a provider class which creates Singleton forms for us. sort of like a singleton factory with a few little tricks to ensure only valid forms are around. I 've seen code before for this on various blogs/message boards, but none of them are as complete as this one.

What's the difference? It's the use of generics that makes this implementation really handy. By using a generic method, we can return a form of the correct type: There's no need to worry about casting.

Another modification is the addition of a parameters to the getinstance method. although this is a little Dodge, and cocould break things if you aren't careful, I 've found it handy when used with a form whose constructor required extra arguments.

So here it is:

View plaincopy to clipboardprint?
  1. Class singletonformprovider
  2. {
  3. Static dictionary <type, form> mtypeformlookup = new dictionary <type, form> ();
  4. Static public t getinstance <t> (Form owner)
  5. Where T: Form
  6. {
  7. Return getinstance <t> (owner, null );
  8. }
  9. Static public t getinstance <t> (Form owner, Params object [] ARGs)
  10. Where T: Form
  11. {
  12. If (! Mtypeformlookup. containskey (typeof (t )))
  13. {
  14. Form F = (form) activator. createinstance (Typeof (t), argS );
  15. Mtypeformlookup. Add (Typeof (t), F );
  16. F. Owner = owner;
  17. F. formclosed + =New formclosedeventhandler (remover );
  18. }
  19. Return (t) mtypeformlookup [typeof (t)];
  20. }
  21. Static void remover (Object sender, formclosedeventargs E)
  22. {
  23. Form F = senderAs form;
  24. If (F = NULL) return;
  25. F. formclosed-=New formclosedeventhandler (remover );
  26. Mtypeformlookup. Remove (F. GetType ());
  27. }
  28. }

And to use it:

View plaincopy to clipboardprint?
    1. Aboutbox abox = singletonformprovider. getinstance <aboutbox> (this );
    2. Abox. Show ();

Well, there you have it. A handy generic Singleton form provider for C #.

Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.