Object Without Reference

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

Hi,

Given the following logic:

1. Determine if an object or process exists.
2. If no, create or launch.
3. If yes, access.

Easy enough to do provided an explicit object reference is
in scope. But what if it's not...

The best example I can think of is "Help". You may wish
to access a help tool from any of many contexts. If the
tool isn't running, it would launch. If it is already
running, the appropriate topic is displayed. We wish to
share a single object or process with all contexts, but we
don't want to drag around object references everywhere.

Can this kind of thing be done with the Process class?

Any advise would be appreciated...

Randy
 
Randy said:
Hi,

Given the following logic:

1. Determine if an object or process exists.
2. If no, create or launch.
3. If yes, access.

Easy enough to do provided an explicit object reference is
in scope. But what if it's not...

The best example I can think of is "Help". You may wish
to access a help tool from any of many contexts. If the
tool isn't running, it would launch. If it is already
running, the appropriate topic is displayed. We wish to
share a single object or process with all contexts, but we
don't want to drag around object references everywhere.

Can this kind of thing be done with the Process class?

Any advise would be appreciated...

What you need is a singleton. Perhaps a late-instanced singleton, or a
pseudo singleton.

Here's a singleton

Class MySingleton
Public Shared ReadOnly instance as MySingleton = new MySingleton()
private Sub New()
end sub

Public sub DoSomething()
'do something here
end sub
End Class

This is a simple singleton class. Notice the private constructor and the
shared readonly field.
The instance of the singleton type will be created when the type loads, and
all threads and scopes will have shared access to the instance.


A late-initialized singleton has the same semantics as the singleton, except
the object instance is not created until it's first used. Use this pattern
when you have to control when the object is first created.


Class MySingleton
Private Shared lock As New Object()
Private Shared instance_ As MySingleton
Public Shared ReadOnly Property instance() As MySingleton
Get
If instance_ Is Nothing Then
SyncLock lock
If instance_ Is Nothing Then
instance_ = New MySingleton()
End If
End SyncLock
End If
Return instance_
End Get
End Property
Private Sub New()
End Sub

Public Sub DoSomething()
'do something here
End Sub
End Class


A pseudo singleton is not really a singleton at all, because you can have
multiple instances of the type, it just uses a shared field called
"instance" to share an object instance among scopes and threads.


Class MySingleton
Public Shared instance as MySingleton

public Sub New()
end sub

Public sub DoSomething()
'do something here
end sub
End Class

Notice the public constructor, and that instance is not ReadOnly.

You can then create an instance of the type and set it as _the_ instance.
Later you can change it.

MySingleton.instance = new MySingleton()

David
 
Hi Randy,

If you're talking about a process then presumably you have some
information about it, such as the exe name or title bar. In which case, yes,
Process will be of use.

There's a thread here, "Preventing multiple instances of windows forms
app", which may be of some use.

If you're talking about some Windows object then you'll probably have to
get 'appy with the Api.

If you're talking about some object of your own making then David's
singleton, which is new to me, looks interesting.

Regards,
Fergus
 
David,

Just what I was looking for.

Thanks a million.

I'm still curious about using the Process class. I know
how to check if a process is running, but I'm wondering if
there is a way to access one of it's methods if it is.

Randy
 
David's singleton approach is perfect.

I'm still curious about the Process class. I know how to
determine if a process is running. The question is, can
one access its public methods via the process object.

The "process" would be an exe of my design.

Randy
 
Nope, it's not that simple I'm afraid. For code security reasons you can't
just "access" a loaded applications public members, you need to know
slightly more information about it. I had a similar task for an application
of mine, I wanted only 1 instance to be loaded and every other instance
started to communicate to the loaded instance and pass it some information,
this was all done via .NET remoting and using Mutex's. Out of curiosity is
the application that you wish to access your application? If not you're on
a dodgy wicket!

Nick.
 
2. Provide a means for a file to be included in a project
without it actually residing in the project folder.
Either that or rename "Add an Existing Item" to something
more appropriate like "Clone an Existing Item and Create a
Maintenance Nightmare".

Yes, I totally agree!! Or the the way it loads up code from an included
project to debug it even though you have a separate instance of VB opened
alread with the source in it! Damn thing, that allways pi55es me off!

Nick.
 
Back
Top