Solved

Problem with Environment.CommandLine routine.

Posted on 2006-07-13
7
201 Views
Last Modified: 2012-05-05
I have an app that is controlled by a module to make sure it is only opened once. Now that works fine but the problem is if my app is open, and a command argument, Environment.CommandLine, is sent to it, nothing happens. The code below just shows the Module to open my app.

(Thanks to Yzlat for the following code)
_________________________________________________________
Module monitor
    Sub main()
        Dim bAppOwnership As Boolean = False
        Dim sAppName As String = "Local\" & Reflection.Assembly.GetExecutingAssembly.GetName.Name
        'Return value - contains true if the calling thread was granted initial ownership of the mutex.
        Dim bAppFirstInstance As Boolean
        'Static ensures the mutex is not garbage collected until the application is unloaded
        Static m As System.Threading.Mutex
        Try
            'Create mutex
            m = New Threading.Mutex(bAppOwnership, sAppName, bAppFirstInstance)
            'If this is the first instance of the mutex, then run the application
            If bAppFirstInstance Then Application.Run(New frmConfig)
        Catch ex As Exception
            'error - display a system modal error message box
            MsgBox(ex.Message, Microsoft.VisualBasic.MsgBoxStyle.SystemModal Or Microsoft.VisualBasic.MsgBoxStyle.Critical Or Microsoft.VisualBasic.MsgBoxStyle.OKOnly, _
            Application.ProductName & " Is Unable To Start. ")
        Finally
            m = Nothing
        End Try
    End Sub
End Module
____________________________________________________________________

Any ideas?
0
Comment
Question by:cjinsocal581
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 41

Expert Comment

by:graye
ID: 17104798
Huh?  What's that got to do with command-line arguments?
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 17109598
Actually, EVERYTHING.

You see, the problem is that this "new" code (Above) that I added to my application DOES prevent any new instances from opening when the app is already opened. The problem is that I have a simple Environment.CommandLine argument that is sent to the app.

Now, when the application IS NOT open, and I send the Environment.CommandLine argument to it, it opens fine and processes the Environment.CommandLine argument. BUT, when the application IS already open, it will NOT accept any new arguments.

So, here is the scneario:

APP IS NOT OPEN:
Using the Run box. I type MyApp.exe -Information

App opens, processes "Information" into a listbox.

APP IS ALREADY OPEN:
Using the Run box. I type MyApp.exe -Information

NO new instance of the app opens (This is good) BUT DOES NOT process "Information"

Ideas?
0
 
LVL 41

Expert Comment

by:graye
ID: 17109789
OK, I understand now.  But this is not gonna be easy...

You'll have to have the 2nd instance of the program "talk to" the first instance (to tell it the change in the command line arguments).  The 1st instance would then stop doing whatever it's doing, and start doing whatever the new command line argument is asking it to do.  After that, the 2nd instance can just exit.

There are several methods of Inter Process Communication (IPC) that are available to VB.Net, but they are all a bit complicated.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/interprocess_communications.asp

This might seem like a crazy idea, but can you simply kill off the 1st instance and completely repace it with the 2nd instance (with the new command line arguments)?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:cjinsocal581
ID: 17109884
Thanks for the response..

To answer your question about killing the current app: No - due to a number of things:

There is a status window in the app that logs all activity of the application, there is a splash window that opens each time the app runs, the app will usually sit in the system tray 99% of the time.
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 17109897
What if I create a second app that just does the monitoring of the CommandLine arguments and pass it to the app? (Plus check to see if it is already opened or not?)

I know this may walk us down that same path, but at least I can kill that one.... Let me know your thoughts
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 17111033
How time critical is the switch to the new "context"?

A simple solution could be this:

1) When the application starts, it record the command line arguments to a common file (perhaps with a file lock or time stamp)
2) If the file already exists (or has a lock) then you know another instance is running and you can exit the current instance
3) Then the application periodically checks this file to see if the content has changed (perhaps by the time stamp).  If it has changed, then that's an indication to stop doing whatever-it-is-that-it's-doing and do something else.

That way this file now serves two purposes... to detect if an 2nd instance exists or not...and to communicate the "change in context" brought on by the 2nd instance's command line arguments.

This could be accomplished with something as simple as a multi-threaded timer looking for changes to the file or by something a bit more elegant (like using the FileSystemWatcher class)
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 17111696
Good thoughts.... I will have to experiment.

Thanks!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question