By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

Referencing application though VB.NET

P: n/a
I am currently developing an application in VB.NET that uses the Microstation's object model via COM Interop.

I have a problem regarding multiple instances of the Microstation application (ustation.exe) and calling the COM object in multiple forms in my application.

The Microstation Application object is demnsioned in Common.mod

Public oMSTN As MicroStationDGN.Application
My application is entered in StartUp.mod and there is a routine that determines if Microstation is already running or not. Different forms load depending on if the user already has an instance if Microstation running or not.
##############################################
# CODE EXECUTED IN STARTUP.MOD #
# TO START MICROSTATION IF IT ISN'T #
# ALREADY RUNNING #
# #
# oMSTN DIMENSIONED IN COMMON.MOD #
# Public oMSTN As Application #
##############################################
Private Sub CheckForProject()
If IsProcessExecuting("ustation") = False Then
projectForm = New frmProj
projectForm.ShowDialog()
Else
'process is executing so get the project
'configuration file currently selected
'and set project ID from database

oMSTN = New MicrostationDGN.Application
sConfigName = oMSTN.ActiveWorkspace.ConfigurationVariableValue(" _USTN_PROJECTNAME")

lProj_ID = GetProjIDFromDb(sConfigName)
End If
End Sub

If Microstation is running then a new instance of oMSTN must be started so that my application can read a microstation configuration variable. That works fine, howver, when other forms load I don't know how to reference the same instance of oMSTN. Starting new instances of oMSTN in each form simply starts and extra instance of ustation.exe running in task manager.

How do I start a call this code once "oMSTN = New MicrostationDGN.Application" in my startup module and then use oMSTN in every form that loads after that?

Thanks in advance

Russ
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
How stupid of me. I'll try this but may need to call on you for some more
help.

Thanks

Russ
"Jonas Pohlandt" <j.******************@dbit-systems.de> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
http://c2.com/cgi/wiki?VisualBasicSingleton

HTH Jonas

Nov 21 '05 #3

P: n/a
Even better (VB.Net specific):
http://www.ondotnet.com/pub/a/dotnet...singleton.html
Nov 21 '05 #4

P: n/a
Thanks for this. I'm starting to understand and I had a go but I was still
struggling a little. I couldn't access to object model of Microstation
through this singleton class that I built. So that I can understand this I
am starting with a basic example, but what might that class look like if it
were to be added to the following simple app?

Russ
Public Class Form1
Inherits System.Windows.Forms.Form

Private oWinWord As Word.Application

#Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
oWinWord = New Word.Application
oWinWord.Visible = True

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oWinWord = New Word.Application

oWinWord.Documents.Add()

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub

End Class
"Jonas Pohlandt" <j.******************@dbit-systems.de> wrote in message
news:en**************@TK2MSFTNGP10.phx.gbl...
Even better (VB.Net specific):
http://www.ondotnet.com/pub/a/dotnet...singleton.html

Nov 21 '05 #5

P: n/a
Well, the purpose of the singleton pattern is to make sure that you are
allways working with only one instance of a class.
I thought this was what you wanted. Applied to your word example it would be
something like this.

public class WordAppSingleton

private shared _myInstance as Word.Application

public shared function GetInstance() as Word.Application
if _myInstance is nothing
_myInstance = New Word.Application 'TODO: Add check if App is
allready running
end if

return _myInstance
end function

end class

Now, if you want to work with the instance of your word application you
would use the shared function WordAppSingleton.GetInstance() to get it, like
this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()
oWinWord.Visible = True

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()

oWinWord.Documents.Add()

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub

So you won't be needing a global oWinWord variable anymore. Use local
variables and WordAppSingleton.GetInstance().
Is this what you need?
Nov 21 '05 #6

P: n/a
That's very clear, thank you so much
"Jonas Pohlandt" <j.******************@dbit-systems.de> wrote in message
news:OW**************@TK2MSFTNGP14.phx.gbl...
Well, the purpose of the singleton pattern is to make sure that you are
allways working with only one instance of a class.
I thought this was what you wanted. Applied to your word example it would be something like this.

public class WordAppSingleton

private shared _myInstance as Word.Application

public shared function GetInstance() as Word.Application
if _myInstance is nothing
_myInstance = New Word.Application 'TODO: Add check if App is
allready running
end if

return _myInstance
end function

end class

Now, if you want to work with the instance of your word application you
would use the shared function WordAppSingleton.GetInstance() to get it, like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()
oWinWord.Visible = True

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & " documents open"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()

oWinWord.Documents.Add()

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & " documents open"
End Sub

So you won't be needing a global oWinWord variable anymore. Use local
variables and WordAppSingleton.GetInstance().
Is this what you need?

Nov 21 '05 #7

P: n/a
One last thing though:

When you use the singleton pattern with an object that holds a reference to
an external Application (like word or microstation) you should
allways have in mind, that a user may shut down the external program
manually. Better check if the app you're working with is still running in
the
GetInstance() Method (which can be a HPITA if you're working with Excel et
al).

"Russ Green" <ru**@NO-SPAMrussgreen.net> schrieb im Newsbeitrag
news:eD**************@TK2MSFTNGP10.phx.gbl...
That's very clear, thank you so much

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.