Link to home
Start Free TrialLog in
Avatar of thorv71
thorv71Flag for Norway

asked on

How to check and use an already open vb.net application from vb.net with parameters

I'm building an application where I now get parameters like "myfile.exe" /test.
This works when i get the parameters in the load section in start form, but the problem accurs when the application already is open, because when I then try to use "myfile.exe" /test from the another application, the "myfile.exe" starts a new instance, and I need then to use the existing instance and still make it posible to send and receive new parameters between the application. How to do this ??
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thorv71

ASKER

I now tried to use "Single instance application"
and the StartupNextInstance in ApplicationEvents, but nothing happens when I try to start the application the second time :-(

Her is the code:

Imports System.Globalization
Imports Microsoft.VisualBasic.ApplicationServices

Public Class MyApplication

    Public Sub Me_StartupNextInstance(ByVal sender As Object, ByVal e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance

        Dim test As String = ""

        If e.CommandLine.Count > 0 Then
            Test = e.CommandLine.Item(0).ToString()
            MessageBox.Show(test)
        End If

    End Sub

    ' Declaration
    Public Event StartupNextInstance(ByVal sender As Object, ByVal e As StartupNextInstanceEventArgs)

End Class
If I remember correctly, this has to be tested outside Visual Studio (both instance). Is this how you try to test it?
Avatar of thorv71

ASKER

Yes, I build the exe file and started it from a shourtcut on desktop serveral times.
And nothing happens the second time.
Avatar of thorv71

ASKER

Her is the shortcut: "D:\VB.NET\Kalkulasjon\bin\Debug\Kalkulasjon.exe" testvar
add a messagebox at the beginning of your event to see what is received
Avatar of thorv71

ASKER

I have tried that already and nothing happens :-(

It's looks like the application don't load ApplicationEvents at startup ever.
Avatar of thorv71

ASKER

Is it possible that the class or public name has to be another name ?
download my demo code and try it.
Instead of writing your own class, use the one that is provided for you.

Go to Project --> Properties.
In the bottom right of the default Tab, click the "View Application Events" button.
Then across the top, change the LEFT dropdown to "(MyApplication Events)", and the RIGHT to "StartupUpNextInstance".
Avatar of thorv71

ASKER

When i set the Left Dropdown to "MyApplication Event", I only get the (Declarations) in the right dropdown :-(
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thorv71

ASKER

Thanks !!!!! :-)

Now it works, but I also had to delete ApplicationEvents.vb file before I enabled the Framework again, then when I enabled the framework it generatet a new file.
Right...sometimes you might want to create your own implementation as emoreau suggested.  Just depends what you're doing.  In many cases, though, you just need the built-in one.
Avatar of thorv71

ASKER

Thanks very much to you both!

Emoreau got me on the right road using "Single instance application" and StartupNextInstance, and Idle_Mind fixed the last annoying error for me :-)