Want to create only ONE instance of class 
Author Message
 Want to create only ONE instance of class

Hi,

I wanted to create a COM object in VB that is to be run on MTS.

Now my requirement is that only one instance of this class should be created
and all applications that request services from this class should use this
one instance of the class.

So I made a ActiveX DLL project, and defined the methods and properties for
my COM object in a class named "Cache". I set the instantiation property of
this class to Public not creatable.

Next I made a wrapper class named "Process". Instantiation for this was set
to "Multiuse".
Within this I made a method to create and pass an instance of the Cache
class.

What I want is that the first time this method is called by any external
program, it makes a new instance of the Cache class, and for all subsequent
requests by all external processes, an pointer to this existing instance is
returned.

Now this code seems to run perfectly in the IDE. However, when I compile the
DLL and call it from different apps, then it creates a new instance of the
Cache class for each application.

Why is this happening and how do I solve the problem.

--

Jayant



Sun, 30 Nov 2003 13:37:18 GMT  
 Want to create only ONE instance of class
Sorry Jayant, you're out of luck.

The only way you can get single instance object (Singleton) is make a
single-threaded ActiveX EXE component. That means you can't put it into MTS.
Objects created in MTS runs on different threads and every thread has it's
own copy of variables in .bas module, so every object starting on new thread
instantiates it's own copy of Cache object.

For caching data in MTS there's Shared Property Manager, have a look at it.
--
HTH,
Alexander Shirshov, MCSD


Quote:
> Hi,

> I wanted to create a COM object in VB that is to be run on MTS.

> Now my requirement is that only one instance of this class should be
created
> and all applications that request services from this class should use this
> one instance of the class.

> So I made a ActiveX DLL project, and defined the methods and properties
for
> my COM object in a class named "Cache". I set the instantiation property
of
> this class to Public not creatable.

> Next I made a wrapper class named "Process". Instantiation for this was
set
> to "Multiuse".
> Within this I made a method to create and pass an instance of the Cache
> class.

> What I want is that the first time this method is called by any external
> program, it makes a new instance of the Cache class, and for all
subsequent
> requests by all external processes, an pointer to this existing instance
is
> returned.

> Now this code seems to run perfectly in the IDE. However, when I compile
the
> DLL and call it from different apps, then it creates a new instance of the
> Cache class for each application.

> Why is this happening and how do I solve the problem.

> --

> Jayant



Mon, 01 Dec 2003 21:38:51 GMT  
 Want to create only ONE instance of class
As far as I know, when you run it in VB, all things behave like they are
in-process servers.  Once you get out of VB, the instancing properties come
to play.  The point is that each thread will hold its own global variables
and that this way there will be multiple instances of your class, all being
in a different appartment.
Singletons are very hard to accomplish in VB.  I myself have a desktop
application and am forced to put all my components in Single-Threated mode.
That way I am sure a singleton is a singleton.
With you problem under MTS I don't know how to do it.

The m{*filter*}(which you won't like) : try avoiding the singleton.


Quote:
> Hi,

> I wanted to create a COM object in VB that is to be run on MTS.

> Now my requirement is that only one instance of this class should be
created
> and all applications that request services from this class should use this
> one instance of the class.

> So I made a ActiveX DLL project, and defined the methods and properties
for
> my COM object in a class named "Cache". I set the instantiation property
of
> this class to Public not creatable.

> Next I made a wrapper class named "Process". Instantiation for this was
set
> to "Multiuse".
> Within this I made a method to create and pass an instance of the Cache
> class.

> What I want is that the first time this method is called by any external
> program, it makes a new instance of the Cache class, and for all
subsequent
> requests by all external processes, an pointer to this existing instance
is
> returned.

> Now this code seems to run perfectly in the IDE. However, when I compile
the
> DLL and call it from different apps, then it creates a new instance of the
> Cache class for each application.

> Why is this happening and how do I solve the problem.

> --

> Jayant



Tue, 02 Dec 2003 03:30:04 GMT  
 Want to create only ONE instance of class
But you can put it into a COM + package....


Quote:
> Sorry Jayant, you're out of luck.

> The only way you can get single instance object (Singleton) is make a
> single-threaded ActiveX EXE component. That means you can't put it into
MTS.
> Objects created in MTS runs on different threads and every thread has it's
> own copy of variables in .bas module, so every object starting on new
thread
> instantiates it's own copy of Cache object.

> For caching data in MTS there's Shared Property Manager, have a look at
it.
> --
> HTH,
> Alexander Shirshov, MCSD



> > Hi,

> > I wanted to create a COM object in VB that is to be run on MTS.

> > Now my requirement is that only one instance of this class should be
> created
> > and all applications that request services from this class should use
this
> > one instance of the class.

> > So I made a ActiveX DLL project, and defined the methods and properties
> for
> > my COM object in a class named "Cache". I set the instantiation property
> of
> > this class to Public not creatable.

> > Next I made a wrapper class named "Process". Instantiation for this was
> set
> > to "Multiuse".
> > Within this I made a method to create and pass an instance of the Cache
> > class.

> > What I want is that the first time this method is called by any external
> > program, it makes a new instance of the Cache class, and for all
> subsequent
> > requests by all external processes, an pointer to this existing instance
> is
> > returned.

> > Now this code seems to run perfectly in the IDE. However, when I compile
> the
> > DLL and call it from different apps, then it creates a new instance of
the
> > Cache class for each application.

> > Why is this happening and how do I solve the problem.

> > --

> > Jayant



Thu, 04 Dec 2003 10:24:17 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. create instance of a class from a string

2. Create an instance from the class name?

3. Using reflection to create class instance

4. Creating a new instance of Winsock in a Class Module

5. ASP - create in just script instance of class provided in current page with different language

6. Using New to create instances of a class in MTS componant

7. Using New to create instances of a class in MTS componant

8. Create Instance of class in running activeX.exe??

9. Create an instance of a Timer obeject in a class

10. dynamically creating instances of classes

11. Restricting to create only one instance of a Windows Form

12. Create only one instance of a form

 

 
Powered by phpBB® Forum Software