473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Object Without Reference

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

Nov 19 '05 #1
6 1886

"Randy" <ra**********@comcast.net> wrote in message
news:04****************************@phx.gbl...
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
Nov 19 '05 #2
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
Nov 19 '05 #3
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
-----Original Message-----

"Randy" <ra**********@comcast.net> wrote in message
news:04****************************@phx.gbl...
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 apseudo 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 theshared readonly field.
The instance of the singleton type will be created when the type loads, andall threads and scopes will have shared access to the instance.

A late-initialized singleton has the same semantics as the singleton, exceptthe object instance is not created until it's first used. Use this patternwhen 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 havemultiple 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
.

Nov 19 '05 #4
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
-----Original Message-----
Hi Randy,

If you're talking about a process then presumably you have someinformation 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 formsapp", which may be of some use.

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

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

Regards,
Fergus
.

Nov 19 '05 #5
Nak
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.

"Randy" <ra**********@comcast.net> wrote in message
news:02****************************@phx.gbl...
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
-----Original Message-----
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
.

Nov 19 '05 #6
Nak
> 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.
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
1
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
11
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
4
by: johnxhc | last post by:
We have a project in .NET 1.1 , some of the .NET methods take COM interface reference pointer as a parameter, somehow we have to call Marshal.ReleaseComObject to release the COM Object, otherwise...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.