cjinsocal581
asked on
Problem with Environment.CommandLine routine.
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.GetExe cutingAsse mbly.GetNa me.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(bAppOwners hip, 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.MsgB oxStyle.Sy stemModal Or Microsoft.VisualBasic.MsgB oxStyle.Cr itical Or Microsoft.VisualBasic.MsgB oxStyle.OK Only, _
Application.ProductName & " Is Unable To Start. ")
Finally
m = Nothing
End Try
End Sub
End Module
__________________________ __________ __________ __________ __________ __
Any ideas?
(Thanks to Yzlat for the following code)
__________________________
Module monitor
Sub main()
Dim bAppOwnership As Boolean = False
Dim sAppName As String = "Local\" & Reflection.Assembly.GetExe
'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(bAppOwners
'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.MsgB
Application.ProductName & " Is Unable To Start. ")
Finally
m = Nothing
End Try
End Sub
End Module
__________________________
Any ideas?
Huh? What's that got to do with command-line arguments?
ASKER
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?
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?
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)?
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)?
ASKER
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.
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.
ASKER
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
I know this may walk us down that same path, but at least I can kill that one.... Let me know your thoughts
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good thoughts.... I will have to experiment.
Thanks!
Thanks!