locked
How to check if an instance was created alrady. RRS feed

  • Question

  • Hi,

    How to check if an instance was created already. Ex: If I create an instance and than if I try to create another instance, it will check if it was created already than display an error message.

     

    Thank you,

    Wednesday, January 17, 2007 4:56 PM

All replies

  • Hi,

      check for another instance of what, your application or some type in your application?  If you want to see if there is already an instance of your app running, then look at this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1119101&SiteID=1  if you are talking about a single instance of a certain type then you want to use the Singleton pattern: http://www.dofactory.com/Patterns/PatternSingleton.aspx

     

    Mark.

    Wednesday, January 17, 2007 5:06 PM
  • It its question about single instance of any class within the application then use Singleton (creational) pattern, please refer the following links for the sample:
    http://msdn2.microsoft.com/en-us/library/ms998426.aspx
    http://msdn2.microsoft.com/en-us/library/ms998558.aspx

    If its single instance at application level means if you want your exe should run only as single instance and you want to restrict the user to open multiple same application, and if one application is running then on another try to open that application should the bring running application to the front,
    Diff ways to do that,
    http://www.codeproject.com/csharp/restricting_instances.asp
    http://www.codeproject.com/csharp/singleinstance.asp
    P/invoke way: http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711

    HTH,

    Wednesday, January 17, 2007 9:07 PM
  • Hi,

    You can do so by choosing the right design.

    In your case I would choose the singleton design pattern.

    If you are not familiure with this pattern I would love to give you an example.

    In any other case, you will need to share a flag or info with the rest of the application. for example you can create a static member in that class that will indicate if it has beed initialized or not. another option is to save the data in the cache/application/session and exc.

    So to sum things up, it is all about the way you design it.

     

    hope this helps,

    guy kolbis

    Wednesday, January 17, 2007 9:10 PM
  • HI,

     

    That would be great, if you could explain me what  pattern is. and how would I save data in cache, application and what is exc.  I know how to save in session.

     

    Thanks alot.

     

    Monday, January 22, 2007 2:49 PM
  • Hi,

      a pattern in software is typically is referred to as a Design Pattern.  These are patterns that come up over and over again in software.  Like in other disciplines such as building a bridge there are certain patterns you can use to build a bridge, a supsension bridge, an arch bridge etc the same is for software, such as wanting a single instance of a type.  For examples of design patterns see:http://www.dofactory.com/Default.aspx

     

    For the cache and application are you talking about ASP.Net programming?  If so you should ask in an ASP forum where you will get more answers that the general C# newsgroup.

     

    Mark.

    Monday, January 22, 2007 4:39 PM