[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

VB Application

Suppose I have an MDI application written in VB. This application can be executed with command line parameter which is the name of the document to open.
The question is if the application is already running and I execute "MyApp.exe /Mydocument.xxx" how can I make the new instance detect that there is an already running instance (The easy part of the question "App.PrevInstance") and force the already running instance to open the document, pointed to in the command line?
0
Herzalah
Asked:
Herzalah
  • 3
  • 2
1 Solution
 
TimCotteeCommented:
You are right about the first part, the second bit is up to you. You need to begin some form of conversation between the two instances. There are a number of ways that you could do this, Windows Sockets (winsock), DDE or API messaging. The simplest is perhaps DDE. If you set the form's LinkMode property to True you can then add a textbox somewhere which can act as a DDE source/destination. In the second instance of the application, use a different textbox with the dde link information of the first to send the filename to the original instance. The Chande event of this textbox can be used to run the appropriate procedure to open the file as you would normally.
0
 
CArnoldCommented:
Herzalah,
   I think I may have a solution to your problem.  I encountered a need similar to what you are asking for when App.PrevInstance proved futile in NT Terminal Server.
   What I did was create a DLL with 2 parameters.  The 1st parameter is App.EXEName.  The 2nd was the text that would appear in the application's title bar (usually, App.Caption would work here).
   You would declare an instance of this DLL in the startup Form or .bas module.  Reference it in the Form_Load (or Sub Main(), if starting from a .bas module).

   The code requires a lot of API calls.  The DLL I mentioned is responsible for acquiring information of a possible previous instance of the application running.  IT does this by:
   1) Grabbing the user's Logon ID and the app's App.EXEName
   2) Creating a text file (AS RANDOM) and writing something to it (Date, Additional parameter values, etc.)
   3) Close and open the text file as READ ONLY under a new file #.  LOCK the text file!
   4) Include and ON ERROR condition at the beginning of the procedure.  When another instance of the application tries to run, it will try to write to the textfile that is locked.  This will create an error.  The ON ERROR procedure should recognizes this error returns a value indicating that another instance of the application is running.  If the application with locked control of the text file is prematurely terminated, the text file becomes unlocked, eliminating possibility of your application falsely reporting an previous instance of an app.

If the application exists, the DLL passes the 2nd parameter to an .exe that is responsible for maximizing and bringing focus to the app.
   I chose to make an .exe file responsible for the maximizing and focusing of the app because an out-of process component was required.  When I tried incorporating this feature into the original DLL, sometimes the app I was trying to prevent from being fully executed was being maximized before terminating.

   If you post your e-mail address, I could e-mail you (and anyone else interested) the source code for the procedures I've mentioned.
0
 
TimCotteeCommented:
Herzalah, a response from you would be appreciated.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
HerzalahAuthor Commented:
Sorry guys for staying far away from this questions. AT the same time I appreciate the efforts you are putting in this.
0
 
CArnoldCommented:
?????!
0
 
TimCotteeCommented:
What can we do for you Herzalah?

To give you some code to achieve this:

Here is is in its simplest form:

Private Sub Form_Load()
  If App.PrevInstance Then
      Text2.LinkTopic = "TDDE|Form1"
      Text2.LinkItem = "Text1"
      Text2.LinkMode = vbLinkAutomatic
      Text2.Text = InputBox("Send Message", "To Previous Instance", "A Message")
      Text2.LinkPoke
      End
  End If
End Sub

Private Sub Text1_Change()
  MsgBox Text1.Text
End Sub

Where TDDE is the name of the executable.  

Another method which is significantly more complex but you wish to consider:

http://vbaccelerator.com/codelib/ssubtmr/startup.htm 
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now