I'm trying to invoke a series of small external console apps from inside a plugin. I want them to run without a window appearing and I'd like to redirect the console output to a file. Ideally, having the option of running them in the background would be nice. I'd also like to keep the apps very simple - without requiring the passing of multiple arguments, etc. This thread, which describes a proposed architecture for "mini-plugins" to feed the UbuStream plugin, will explain why I need to do this.
I have tried two approaches but am still unable to come up with a solution that fits the above criteria. One approach:
runs the app OK and, by removing the WaitForExit() line, I can run it as a background process. But the myArgs string is passed to the app as an argument instead of being interpreted by the shell, so the stdout is not redirected and no file is created. (Also, running in a minimized command shell window causes the apps to show up in the Windows task bar while they run. Irritating but not a show stopper.) The app completes execution fairly quickly, however.
So I tried this approach:
This redirects the output to the file OK and the app runs invisibly (nothing in the task bar even) but it can't be run in the background since I'd need to get rid of the WaitForExit() line but then I wouldn't know when to write the captured output to the file. The command line ">" approach in the first example would have solved this by sticking the file output functionality into the shell process itself but the .Net "shell" doesn't seem to work like a real shell and do the right thing.
Interestingly, using the second approach, the app takes forever to execute. I mean waaaay longer than when run using ShellExecute. Which is wierd because you'd think the shell would add some overhead so running without it would be faster. (And, of course, you can't use RedirectStandardOutput unless UseShellExecute is false - Catch 22!)
I know that I could solve this with brute force and ignorance by having a switch in the target app, triggered by passing an argument, that would explicitly write execution log lines to a log file instead of to the console. But, as I said, I want to keep the design of the apps as simple as possible and with a minimum of confusing argument options.
Any ideas? Sympathy, even?
I have tried two approaches but am still unable to come up with a solution that fits the above criteria. One approach:
Code:
String myArgs = "> log.txt";
ProcessStartInfo startInfo = new ProcessStartInfo(dynSourceApp, myArgs);
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process myProcess = Process.Start(startInfo);
myProcess.WaitForExit();
So I tried this approach:
Code:
ProcessStartInfo startInfo = new ProcessStartInfo(dynSourceApp, "");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Process myProcess = Process.Start(startInfo);
String logLines = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
File.WriteAllText(log.txt, logLines);
Interestingly, using the second approach, the app takes forever to execute. I mean waaaay longer than when run using ShellExecute. Which is wierd because you'd think the shell would add some overhead so running without it would be faster. (And, of course, you can't use RedirectStandardOutput unless UseShellExecute is false - Catch 22!)
I know that I could solve this with brute force and ignorance by having a switch in the target app, triggered by passing an argument, that would explicitly write execution log lines to a log file instead of to the console. But, as I said, I want to keep the design of the apps as simple as possible and with a minimum of confusing argument options.
Any ideas? Sympathy, even?
[SIZE=1]GBPVR v1.3.11 [/SIZE][SIZE=1]HVR-1250, [/SIZE][SIZE=1]ES7300[/SIZE][SIZE=1], 4GB, GeForce 9300, LianLi, Vista.[/SIZE]
[SIZE=1]GBPVR v1.0.08 [/SIZE][SIZE=1]PVR-150, [/SIZE][SIZE=1]P4 2.26GHz, [/SIZE][SIZE=1]1GB,[/SIZE][SIZE=1] GeForce 6200, [/SIZE]Coupden, XP[SIZE=1]
[/SIZE]
Author: UbuStream plugin, UbuRadio plugin, EPGExtra utility.
[SIZE=1]GBPVR v1.0.08 [/SIZE][SIZE=1]PVR-150, [/SIZE][SIZE=1]P4 2.26GHz, [/SIZE][SIZE=1]1GB,[/SIZE][SIZE=1] GeForce 6200, [/SIZE]Coupden, XP[SIZE=1]
[/SIZE]
Author: UbuStream plugin, UbuRadio plugin, EPGExtra utility.