I'm trying to start a C# program running, and then give it command from the cmd.exe after it's started running. For instance, suppose I started my .exe from the command line (C://FILEPATH/my_program.exe). I'd then like to have that program continue running, and then have me be able to pass commands to it that it is capable of handling. In my ideal world this would be something like "C://FILEPATH/my_program.exe run_my_command()" which would execute the run_my_command function, or "C://FILEPATH/my_program.exe k", which would do something in response to the char k that I'd pre-programmed in. I know that, as I've typed, would start a new copy of my_program.exe. I'd only like to have one running while I pass something like that in.
Does anyone know how to do this? Sample code would be wonderfully appreciated. Thanks!!
The simplest solution would be for your second instance of "my_program.exe" to look for an existing instance that's already running, "pass" the message over to it and then exit immediately.
The usual way this is implemented is via named pipes (System.IO.Pipes in .NET 3.5+). When your program starts up, listen on a named pipe with a given name. If there's something else already listening on that pipe, send the message to it and exit.
You are describing a typical service and command tool. The service (demon) runs in the background and executes commands. The command tool takes user commands and passes them to the service. See Windows Service Applications. Having a service instead of starting several processes takes care of some issues your approach has, like security isolation between the processes (eg. one user starts the a command, another user starts another command and gets executed in the context of the first user) and process lifetime issues (user launches a command and then closes his session).
The command tool would communicate with the process via classic IPC (local RPC, pipes, shared memory, etc).
Related
I'm trying to write a Tray-Icon application with WPF as a kind of add-on to a telephone software. However my only way of recieving data from said telephone software is by having it automatically execute a URI with parameters (command line or http). Essentially I can freely define the format similar to this:
trayapp.exe --num $caller.number --name $caller.name
When the telephone software recieves a call it'll automatically fill in the parameters and executes the target.
So basically I want a permanently active executable that's running in the background but my only way of getting the necessary info (like caller-number) is by passing it as a command line parameter.
Is there a good way to get the data that's being passed to a URI into my background application? It's not like I can just re-execute the tray-app, or can I? Communication via HTTP (locally!) seems overkill but I just don't know a windows-internal equivalent ...
I'm writing an embedded powershell host in C# (windows application), and if there is an external program called in the pipeline the console window flashes as it's executed. Ping and netstat are examples of this as shown in the following code snippet
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Get-Content test.txt; ls; ping www.google.com");
PowerShellInstance.Invoke();
# ...more code here to print output etc
}
Once the ping command is reached in the pipeline a console window will popup execute the command and return results. I would like to do this without the console window showing. I've searched and found examples of how to deal with this by starting a process for external commands and redirecting output.
I can do that of course, but then how would I test if a command in the pipeline is a call to an external program?
I would really like to be able to handle this generically if possible; meaning that I don't want to test for specific programs (e.g. ping, netstat, net) if I don't have to. The native powershell.exe does this so it's somehow possible.
It turns out that this is expected behavior of invoking console commands. In Windows 7 and above conhost.exe is responsible for handling calls to console programs. In this case, ping and netstat trigger an instance of conhost to be created, it handles the request, returns the results, and then remains open. conhost.exe starting up is what is causing the console flash I'm seeing. The fact that it remains open is why I was only seeing the console window flash once. Once conhost is connected to the calling process all subsequent console commands are handled by that same instance.
I'm marking this as an answer because a new question should be created to address conhost specifically.
Source. Windows Internals Part 1
I have an issue. I'd have a video player that takes in simple parameters as a c# form app. As an experiment to better understand programming, I'd like to have only one instance of the app running and have it refresh with the new args if the open command is given. I could program it so that when it gets the signal, to refreshVideo() or something like that.
Pseudo example:
//app is started from cmd line
//open app for first time
vidViewer.exe("lotr.avi", "44:44");
//keep instance open but have it refresh with new movie
vidViewer.exe("star_wars.avi", "22:32")
As it stands right now a new app with embedded video player will open, so I could have 100 open flicks if I passed it enough args. I'd just like to keep it at one.
Can provide more info if needed.
Thanks all,
Kevin
My suggestion would be to design your application such that, when launched, it would attempt to acquire a system resource that is mutually exclusive (meaning that only the first instance would succeed). Since, in your scenario, you will also require a means of interprocess communication (to transmit the name and start-time of new videos), you may use the same mechanism for achieving this.
You could, for example, use a self-hosted WCF service, bound to a fixed TCP port, that each application instance attempts to register upon being started. Due to the way ports work, only the first instance will succeed; subsequent instances would fail with a “port already in use” exception.
If an instance manages to register the WCF service, then you may assume it to be the “principal” instance and proceed to play the video on it. It should, however, listen for incoming messages from the WCF service and update the video being played accordingly (see below).
If an instance finds that the port is already in use, it should assume that another instance is already running. It would then initialize a WCF client that sends the name and start-time of the new video to be played to the WCF service. Finally, it should terminate itself without displaying any window, assuming that the principal instance will take care of playing its video.
I have an application that runs as a Windows service. It stores various things settings in a database that are looked up when the service starts. I built the service to support various types of databases (SQL Server, Oracle, MySQL, etc). Often times end users choose to configure the software to use SQL Server (they can simply modify a config file with the connection string and restart the service). The problem is that when their machine boots up, often times SQL Server is started after my service so my service errors out on start up because it can't connect to the database. I know that I can specify dependencies for my service to help guide the Windows service manager to start the appropriate services before mine. However, I don't know what services to depend upon at install time (when my service is registered) since the user can change databases later on.
So my question is: is there a way for the user to manually indicate the service dependencies based on the database that they are using? If not, what is the proper design approach that I should be taking? I've thought about trying to do something like wait 30 seconds after my service starts up before connecting to the database but this seems really flaky for various reasons. I've also considered trying to "lazily" connect to the database; the problem is that I need a connection immediately upon start up since the database contains various pieces of vital info that my service needs when it first starts. Any ideas?
Dennis
what your looking for is SC.exe. This is a command line tool that users can use to configure services.
sc [Servername] Command Servicename [Optionname= Optionvalue...]
more specificly you would want to use
sc [ServerName] config ServiceName depend=servicetoDependOn
Here is a link on the commandlike options for SC.EXE
http://msdn.microsoft.com/en-us/library/ms810435.aspx
A possible (far from ideal) code solution:
In you startup method code it as a loop that terminates when you've got a connection. Then in that loop trap any database connection errors and keep retrying as the following pseudo code illustrates:
bool connected = false;
while (!connected)
{
try
{
connected = openDatabase(...);
}
catch (connection error)
{
// It might be worth waiting for some time here
}
}
This means that your program doesn't continue until it has a connection. However, it could also mean that your program never gets out of this loop, so you'd need some way of terminating it - either manually or after a certain number of tries.
As you need your service to start in a reasonable time, this code can't go in the main initialisation. You have to arrange for your program to "start" successfully, but not do any processing until this method had returned connected = true. You might achieve this by putting this code in a thread and then starting your actual application code on the "thread completed" event.
Not a direct answer put some points you can look into
Windows service can be started Automatically with a delay. You can check this question in SO for some information about it.
How to make Windows Service start as “Automatic (Delayed Start)”
Check this post How to: Code Service Dependencies
We have written an application that sits in the tray controlling OpenVPN as an extension to a bigger application.
If you run openvpn.exe on command line, you can press F4 to close it. We need to do send the same keypress from C#, but you can only send string values to StandardInput.
We have been forced to kill OpenVpn to close it, and this seems to be causing BSOD every now and then on Vista...
Here is a link to my post on MSDN that also describes the issue: MSDN Forums
Does anyone know how to send special keystrokes to a Process with StandardInput?
Or maybe a workaround to close OpenVPN more cleanly?
UPDATE:
The following do not work when passed to StandardInput.Write(), F1 key is in this example:
ConsoleKey.F1
"\x70" (Hex value for F1)
Convert.ToChar((int)ConsoleKey.F1)
We already properly redirect the input/output, because we can successfully pass username/password to OpenVPN with no problem.
UPDATE 2: Found this on some command line option documentation for OpenVPN:
--service exit-event [0|1]
Should be used when OpenVPN is being automatically executed by another program in such a context that no interaction with the user via display or keyboard is possible. In general, end-users should never need to explicitly use this option, as it is automatically added by the OpenVPN service wrapper when a given OpenVPN configuration is being run as a service.
exit-event is the name of a Windows global event object, and OpenVPN will continuously monitor the state of this event object and exit when it becomes signaled.
The second parameter indicates the initial state of exit-event and normally defaults to 0.
Multiple OpenVPN processes can be simultaneously executed with the same exit-event parameter. In any case, the controlling process can signal exit-event, causing all such OpenVPN processes to exit.
How would I use this in C#? Is the "exit-event" signaling they are mentioning a Mutex?
If I run OpenVPN as the following:
"openvpn.exe --config PathToMyConfig.ovpn --service MyEventName 0"
Then the following C# code causes OpenVPN to exit cleanly:
EventWaitHandle resetEvent = EventWaitHandle.OpenExisting("MyEventName");
resetEvent.Set();
Props to consultutah, his comments helped quite a bit.
StandardInput.Write(ConsoleKey.F4);
Obviously you have to get StandardIn for the process.