Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2004

Tip: Looking for answers? Try searching our database.

Referencing application though  VB.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Russ Green - 01 Oct 2004 10:00 GMT
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
Jonas Pohlandt - 01 Oct 2004 10:12 GMT
http://c2.com/cgi/wiki?VisualBasicSingleton

HTH Jonas
Russ Green - 01 Oct 2004 10:18 GMT
How stupid of me.   I'll try this but may need to call on you for some more
help.

Thanks

Russ

> http://c2.com/cgi/wiki?VisualBasicSingleton
>
> HTH Jonas
Jonas Pohlandt - 01 Oct 2004 10:19 GMT
Even better (VB.Net specific):
http://www.ondotnet.com/pub/a/dotnet/2002/11/11/singleton.html
Russ Green - 01 Oct 2004 12:17 GMT
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

> Even better (VB.Net specific):
> http://www.ondotnet.com/pub/a/dotnet/2002/11/11/singleton.html
Jonas Pohlandt - 01 Oct 2004 13:08 GMT
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?
Russ Green - 01 Oct 2004 16:03 GMT
That's very clear, thank you so much

> Well, the purpose of the singleton pattern is to make sure that you are
> allways working with only one instance of a class.
[quoted text clipped - 43 lines]
> variables and WordAppSingleton.GetInstance().
> Is this what you need?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.