Referencing application though VB.NET

R

Russ Green

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
 
R

Russ Green

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

Thanks

Russ
 
R

Russ Green

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
 
J

Jonas Pohlandt

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top