Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I would like to know how its possible to send multiple entries to a process
I have achieved this by opening a process for every file I pass to it but this is very messy and runs to many individual process of the same kind because the sub is called from within a loop.

How can I pass multiple commands to only one instance of the process instead of having to start a new process with every command?...


This might be easy to do as I am sure it is but I cannot find much help because of the nature of this where people only wish to do something such as starting "notepad.exe". My example must first run the process and then it is passed the arguments for that file which is the file path of the process i wish to start and the file path of the file I want to perform work.

(eg.) "C:\folder\process.exe" "C:\folder\Myfile.txt"



thank you in advance..






Here is the code that I have found...



The Client window (Windows form application)
consists of one textbox named txtText and one button named cmdSend
VB
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
Public Class Form1

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Dim ipcCh As New IpcChannel("cmd.exe")
        ChannelServices.RegisterChannel(ipcCh, False)

        Dim obj As SharedInterfaces.ICommunicationService = _
          DirectCast(Activator.GetObject(GetType(SharedInterfaces.ICommunicationService), _
          "ipc://IPChannelName/SreeniRemoteObj"), SharedInterfaces.ICommunicationService)
        obj.SaySomething(txtText.Text)

        ChannelServices.UnregisterChannel(ipcCh)
    End Sub
End Class



The Server console (console application)
Here is the code from the Main.vb
VB
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
 
Module Main
    Sub Main()
        Dim ipcCh As IpcChannel
        ipcCh = New IpcChannel("IPChannelName")

        ChannelServices.RegisterChannel(ipcCh, False)
        RemotingConfiguration.RegisterWellKnownServiceType( _
             GetType(CommunicationService), "SreeniRemoteObj", _
                           WellKnownObjectMode.Singleton)

        Console.WriteLine("Press ENTER to quit")
        Console.ReadLine()
    End Sub
End Module


Also in the console program there is another called CommunicationService.vb
VB
Public Class CommunicationService
    Inherits MarshalByRefObject
    Implements SharedInterfaces.ICommunicationService
    Public Sub SaySomething(ByVal text As String) Implements SharedInterfaces.ICommunicationService.SaySomething
        Console.WriteLine("The client said : " & text)
    End Sub
End Class


And finally a class library part called SharedInterfaces

VB
Public Interface ICommunicationService
    Sub SaySomething(ByVal text As String)
End Interface




My problem is that the program starts the console application from where I have no idea and really what i think i need is to have a process and only one process to start and then pass the strings to it one after another.

thank you for your patients.
Posted
Updated 20-Nov-19 2:45am
v4

1 solution

It can be done using IPC. Any IPC, but, as it happens on on computer, it's the best achieved using remoting with IPC channel, which is actually implemented using named pipes.

I actually have this mechanism implemented. It differs from other methods by one interesting idea: I use the same channel the keep the application in one instance of the process and to pass data (files, typically) from second instance (yes, it can be used at first), and the first one.

Here is the scenario. A started process takes, for example, command-line parameters and tries to connect to remote object which is supposed to be created by the first instance. If the connection fails, it means that the process is only one. In this case, this process creates a remote object. If later you executes the same application, it tries do the same, to connect to remote object. This time, it is successful. This indicates that this instance of the process is the second one. The process sends data to the channel and terminates itself immediately. The first instance of the process reads the data from the channel and processes it.

Catchy, isn't it?

[EDIT]

For command-line parsing, I would offer my CodeProject article: Enumeration-based Command Line Utility[^].

My library is especially easy to use and helps to avoid bugs, as it is not based on hard-coded strings (like keys). You can also see another solution I recommend in my article.

—SA
 
Share this answer
 
v4
Comments
Dale 2012 5-Mar-13 2:29am    
This is very interesting, your saying I can send the filepath and arguments to the IPC channel?..
Sergey Alexandrovich Kryukov 5-Mar-13 11:28am    
Yes, of course. I explained pretty much everything. Is't tiny code. Just learn remoting, it's very lean. Alternative is WCF (self-hosted), which has same kind of channels. I did not use WCF for this purpose, because old remoting is just good enough, and I know cases when remoting was more flexible and gave me extra possibilities, but for this case, and, if by any chance you have experience with WCF, this should be an alternative.

One little secret. You need some string constact to name the channel uniquely. I used System.Reflection.Assembly.GetEntryAssembly().Location for this name (perhaps I had to replace path separator, I just don't remember now). As no other application can have the same location, it is a system-unique name. Of course, if you copy an application to other location, it won't work as single instance, but it's OK — after all, what's the identity of an application. Alternatively, you can sign assembly and use the strong name or public key token for the channel name. This is just the subtlety...

—SA
Dale 2012 5-Mar-13 13:18pm    
Ok I have some reading to do i think I looked into it but now must figure out what parts of the process code I have, how to make work with your example. do you have any simple websites that use this example?..,


thank you I will try
Sergey Alexandrovich Kryukov 5-Mar-13 13:24pm    
Please try.
When (and if) you realize that this is reasonable (it is, works very well), please don't forget to accept this answer formally (green button).
In all cases, your follow-up questions are welcome.
—SA
Dale 2012 5-Mar-13 13:56pm    
I found a small project which uses your example to open a form with a textbox and a button which does what I want but it is a console application that listens for whatever is typed in the textbox. The code is a bit confusing to me because I cannot see where it is call the information the console window is receiving.

I know im still confused about this so I will post the code here in hopes that you will help me to understand how to implement it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900