Accessing a single global variable/object from Multiple Apartments 
Author Message
 Accessing a single global variable/object from Multiple Apartments

I am trying to access a single Global Variable/object from multiple objects
created in different Apartments. Basically I have created an ActiveX exe
with a MultiUse class and have set Thread Per Object option in VB project
Properties. A client object creating a new object out of this class needs to
access this global variable (I know I know there are issues!). Typically
this variable is a Collection and I want to add to this collection from the
client application. Do you know of any way. Please Plese Please help me out
here!
Thanks

Rajesh Jain



Mon, 09 Oct 2000 03:00:00 GMT  
 Accessing a single global variable/object from Multiple Apartments



Quote:
>I am trying to access a single Global Variable/object from multiple objects
>created in different Apartments. Basically I have created an ActiveX exe
>with a MultiUse class and have set Thread Per Object option in VB project
>Properties. A client object creating a new object out of this class needs to
>access this global variable (I know I know there are issues!). Typically
>this variable is a Collection and I want to add to this collection from the
>client application. Do you know of any way. Please Plese Please help me out
>here!
>Thanks

>Rajesh Jain


I could be wrong, but I thought that all public variables inside a
module in your ActiveX EXE ended up being a single instance of the
variable visible by every object created by from the ActiveX EXE.  I
haven't tried it myself, but that's how I'd keep track of things like
'how many objects have been created', etc.

-Kevin Buchan
Senior Developer
Premier Service Systems, Inc.



Tue, 10 Oct 2000 03:00:00 GMT  
 Accessing a single global variable/object from Multiple Apartments

Rajesh,

There may be a way. Most of this was adapted from ideas obtained from VB
Programmers Journal Feb 98 (www.devx.com). Email me if you would like their
source code.

Assuming all the objects that want to access the common object are the same
type, you can create an extra class to handle object creation. Then,
externally to the ActiveX EXE, don't create the multi-threaded class/object
- instead use the extra class to do it for you.

Inside the new class have something like:

public function CreateOriginalObject (byref strOriginalObjectName) as
OriginalObject

Set CreateOriginalObject = CreateObject(ProjectName & "." &
strOriginalObjectName)
call CreateOriginalObject.SetReferenceToGlobal(whatever)

end function

This should create a new instance of MultiThreadedClassName IN A NEW
THREAD, and then the second line can be used to set a reference to
something in the extra class you have added. By storing the object in the
new class and passing in references to each object you create (which are on
different thread), you should have a 'fudged' global variable.

Oh, BTW if you use multi-threading in this way, MS will NOT support
anything. You have been warned.

Get back to me if this helps.

Matthew



Quote:


> >I am trying to access a single Global Variable/object from multiple
objects
> >created in different Apartments. Basically I have created an ActiveX exe
> >with a MultiUse class and have set Thread Per Object option in VB
project
> >Properties. A client object creating a new object out of this class
needs to
> >access this global variable (I know I know there are issues!). Typically
> >this variable is a Collection and I want to add to this collection from
the
> >client application. Do you know of any way. Please Plese Please help me
out
> >here!
> >Thanks

> >Rajesh Jain




Fri, 13 Oct 2000 03:00:00 GMT  
 Accessing a single global variable/object from Multiple Apartments

Rajesh,

I've had a similar situation with trying to interface multi-threaded objects
with a common data storage.  Here is the approach I took, which is VERY similar
to an example project a MSFT guy used in a presentation on how to interface
multi-threaded objects to a OCX control that did not support multi-threaded
access.

Here is the approach:
    Create the "global" class on a thread, either the main thread of the app or
on another thread.

    Create instances of your "worker" classes on their own threads

    Give each "worker" class a reference to the "global" class.  Realize that
all access to the "global" class from the "worker" classes require cross-thread
data marshalling, which has A LOT of time consuming overhead, so minimize the
cross-thread calls as much as possible.

    Now, since the "global" class is running once on its own thread, obviously
it can only do one thing at a time.  So, if it is processing data for worker
class #1 and worker class #2 calls a method of the global class, worker class #2
will be blocked until the global class finishes the task for #1.  If the time
spent in the global class is small relative to the time spent in the worker
classes, this shouldn't be much of a problem, somtehing similar to waiting for
the disk on a file read.

Let me know if this helps you, or if you run into any other problems with this
approach.  I've only done this will small test projects, haven't got my main app
structured good for this yet.

Please respond to the group - I keep my email address of the newgroups becuase
of all the junk mail address harvesters...

Thomas

Quote:

> I am trying to access a single Global Variable/object from multiple objects
> created in different Apartments. Basically I have created an ActiveX exe
> with a MultiUse class and have set Thread Per Object option in VB project
> Properties. A client object creating a new object out of this class needs to
> access this global variable (I know I know there are issues!). Typically
> this variable is a Collection and I want to add to this collection from the
> client application. Do you know of any way. Please Plese Please help me out
> here!
> Thanks

> Rajesh Jain




Fri, 13 Oct 2000 03:00:00 GMT  
 Accessing a single global variable/object from Multiple Apartments

 Well I thought of the same solution myself. But the problem seems to
be as follows:-
        Let us assume that the Global Object class is called clsGlobal. We
create a GlobalObject in a module say modMain.

        There is a class called clsNew which contains a variable defined as

Public theGlobalObject as clsGlobal

What we are propossing now is create a clsFactory which has a method as
follows (for simplicity sakes, we assume that only one class can be created)

Public Function CreateObjectForMe() As clsNew
    Dim theNewObject as clsNew
    Set theGlobalObject=CreateObject("GlobalTestProj.clsNew")
    set theNewObject.theGlobalObject=modMain.GlobalObject
    set CreateObjectForMe= theNewObject
End Function

This structure is now compiled into an ActiveX EXE.
Next I create a project, say Client.EXE. I make a reference to the ActiveX
EXE and do the following (i.e. create a clsFactory object)

Dim x as NEW ActiveXEXE.clsFactory

 The problem is, the moment I do that, I get a reference to an object which
has no knowledge of the GlobalObject created anywhere since that
GlobalObject
is created again!
        By the way, I saw a remark that Microsoft will not support
multithreading written in a particular way. Ofcourse there are issues of
re-entrancy etc. etc. But for simple projects where you need to perform some
operations (long operations), the project can be designed to avoid such
situations. We can use the paradigm of "Let me tell you what to do and when
to do. I will wait (meaning not wait but will not access you) till you tell
me that you are done. Even microsoft can't avoid such things/issues.
        This is my first attempt at writing such a project. I don't know
what
kind of issues I am going to face. The problem at hand is one such
issue.Please
send me any code that you might think will help me out.
        As far as cross-thread marshalling is concerened, somebody was
right! Write code in such a way so that it is minimum. Anyway, I heard from
MSFT that it is no good than Cross-Process marshalling. Well, I am writing a
server kind of an Application which simply processes (or at least seem to
;-)   ) multiple loans at a time! I need it to process in multiple threads
since the
process itself is more or less a sequential process. However multiple such
processes should be able to run simultaneously. That is the objective.

Quote:

>Rajesh,

>There may be a way. Most of this was adapted from ideas obtained from VB
>Programmers Journal Feb 98 (www.devx.com). Email me if you would like their
>source code.

>Assuming all the objects that want to access the common object are the same
>type, you can create an extra class to handle object creation. Then,
>externally to the ActiveX EXE, don't create the multi-threaded class/object
>- instead use the extra class to do it for you.

>Inside the new class have something like:

>public function CreateOriginalObject (byref strOriginalObjectName) as
>OriginalObject

>Set CreateOriginalObject = CreateObject(ProjectName & "." &
>strOriginalObjectName)
>call CreateOriginalObject.SetReferenceToGlobal(whatever)

>end function

>This should create a new instance of MultiThreadedClassName IN A NEW
>THREAD, and then the second line can be used to set a reference to
>something in the extra class you have added. By storing the object in the
>new class and passing in references to each object you create (which are on
>different thread), you should have a 'fudged' global variable.

>Oh, BTW if you use multi-threading in this way, MS will NOT support
>anything. You have been warned.

>Get back to me if this helps.



Fri, 13 Oct 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Accessing a single global variable/object from Multiple Apartments

2. Global Variables / Global Objects

3. Problems with multiple apps. accessing global Printer object

4. Multiple programs accessing single object

5. I need a single global connection object

6. I need a single global connection object

7. Use multiple API calls or Global public variable?

8. Global Variables for multiple forms

9. Multiple forms with global variables

10. Sharing of global multiuse variables over multiple projects

11. Assigning global variables from a table with multiple rows

12. How to define a global variable/functions/procedures for multiple projects(.exe)

 

 
Powered by phpBB® Forum Software