Results 1 to 9 of 9

Thread: How to send instructions to an instance of my application?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    How to send instructions to an instance of my application?

    Hi all,

    I built a CAD application in VB.NET. I takes into consideration the commandLineArgs, so when a user double-clicks on a file saved with my application, it starts my app and opens the said file. (In the same way Word automatically starts and open a .DOC file when you double-click on it...)

    Now the issue I have is when the user double-clicks on a file while my app is already running. I do not want a second instance of my application to run, but rather have the already running instance of my app open the file the user double-clicked on. And I don't know how to do that!

    The first thing my application does when its starts is to verify if another instance of itself is already running. If so, it simply closes after displaying a message to the user (that he/she can't run my app twice at once...) I guess my best bet is to figure out how to send a message from the new instance of my app to the already running one before closing itself...

    Can anybody guide me towards a solution?

    Thanks in advance!


    Alain
    Don't ask why, just reboot!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,729

    Re: How to send instructions to an instance of my application?

    In the Project Properties, tick the "Single Instance" option. This automatically ensures there is only one instance.

    To handle the command line when a new instance would start, use the StartupNextInstance event:
    https://docs.microsoft.com/en-us/dot...ramework-4.7.2
    (to add this event to your project, in Project Properties click on View Application Events)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    Re: How to send instructions to an instance of my application?

    Thanks, but for this one must enable application framework, and it is not my case (my app starts from a 'Sub Main', and not a form...)

    Any other suggestion?
    Don't ask why, just reboot!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    Re: How to send instructions to an instance of my application?

    Thanks, but for this one must enable application framework, and it is not my case (my app starts from a 'Sub Main', and not a form...)

    Any other suggestion?
    Don't ask why, just reboot!

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    59

    Re: How to send instructions to an instance of my application?

    https://stackoverflow.com/questions/...unning-process

    this contains the right answer with the 50 bounty accepted in the bottom you create one of those object passers and pass your document like that to previous running instance of the same program by program process Id

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    Re: How to send instructions to an instance of my application?

    Cool, thanks!

    I'll check this out and report back. That said, re-evaluating the current architecture of my app to switch to a more 'standard' application that has a startup form is also an option!

    Thanks again!
    Don't ask why, just reboot!

  7. #7
    Lively Member
    Join Date
    Jan 2013
    Posts
    124

    Re: How to send instructions to an instance of my application?

    Hi,

    You could use this method. If a user double clicks a file that was saved with your application, a new instance will run and open the file and the previous instance will be closed. Use it if you don't mind this behavior of course.

    Code:
            Dim ArgsExist As Integer = Environment.GetCommandLineArgs().Length
            If ArgsExist > 1 Then
                Dim CurrentProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess()
                Dim CurrentProcessName As String = My.Application.Info.AssemblyName
                For Each PreviousProcess As Process In Process.GetProcesses
                    If LCase(CurrentProcessName) = LCase(PreviousProcess.ProcessName) And CurrentProcess.Id <> PreviousProcess.Id Then
                        PreviousProcess.Kill()
                    End If
                Next PreviousProcess
             Else
                 'Put your current double instance verifying code here
            End If
    Hope that helps

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    Re: How to send instructions to an instance of my application?

    Thanks, but that wouldn't work. I don't want to close the first instance of my app for simlar reasons we wouldn't want a Word document to close itself automatically (without saving on top of it!) when you open a new one.
    Don't ask why, just reboot!

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,648

    Re: How to send instructions to an instance of my application?

    Quote Originally Posted by Alain View Post
    Thanks, but for this one must enable application framework, and it is not my case (my app starts from a 'Sub Main', and not a form...)
    Is there a good reason for that? It's quite possible that what you are doing in that Main method can still be done with the Application Framework enabled. Then again, maybe not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width