I have a winforms application where I run a process (code shown below) in a loop. Suppose I have 10 items, I get 10 command windows where the actions are run. So, I wanted to know if there is a way where I can run the process 10 times but have just one command window open where all my actions are run.
Process p = new Process();
try
{
var pInfo = new ProcessStartInfo(executable, args);
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.WorkingDirectory = workingDir;
p.StartInfo = pInfo;
p.EnableRaisingEvents = true;
SubscribeEvents(p);
p.Start();
p.WaitForExit();
}
I guess if you are doing winforms, you actually already probably have a main window open.
You can use this to hide your other windows:
pInfo.CreateNoWindow = true;
Related
I have an exe file I'm calling with p.StartInfo. This exe takes some time till it finishes, so in the meanwhile I want a 'Wait' window to be on top, and not modal (so the GUI stays responsive).
So this is my code:
Task.Factory.StartNew(() =>
{
Process[] FileProcess = Process.GetProcessesByName("SomeFile.exe");
if (FileProcess.Length == 1)
wait_debug.ShowDialog();
});
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "SomeFile.exe";
p.StartInfo.WorkingDirectory = work_directory;
p.EnableRaisingEvents = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
The problem is the wait_debug window is not always shown, and this is strange, because in the task manager the SomeFile.exe IS running, but nevertheless, the wait window is not shown. It's hard to 'catch' the failures with the debugger, and so far I did not manage doing so. Some times it pops up, sometimes it doesn't. very annoying.
Any idea why, or what is a better solution?
btw, I'm using .Net 4 (I know some solutions only work with 4.5).
Thanks!
I have a C# code that uses command prompt to call a python script. The script currently takes about 25 seconds to run. When run in command prompt, the python script has several outputs until finally outputting "done". My C# code runs the script, but never closes the command prompt if I use "WaitForExit". So my thought is I need some kind of logic to check to see if "done" has been outputted, but the internet has not been very helpful with methodology.
Here's what I have, it currently only outputs the first line, "Microsoft Windows [Version 6.1.7601]". Obviously, no good.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\Optimization";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"ipy spot_for_labview.py");
StreamReader mySR = p.StandardOutput;
string mystr = mySR.ReadLine();
Debug.WriteLine(mystr);
p.WaitForExit(25000); //25000 is a placeholder until better method found.
p.Close();
If there's anyway to close the process after it finishes, or to get all the cmd output I'm all ears.
Any help is appreciated.
did you try this event ? Process.OutputDataReceived Event
or Process.ErrorDataReceived Event
here is the code from MSDN
Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
...
....
...
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
}
I have faced a strange problem when trying to run a process from hidden window - the process I run runs in hidden like my process, Am I doing something wrong? I want to run that child process not hidden.
Process.Start(Path.GetTempPath() + "cleanup.exe", Path.GetDirectoryName(Application.StartupPath);
you can try with creating an object of Process class as below :
Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Passing the batch file/exe name
objProcess.StartInfo.FileName = string.Format(strBatchFileName);
// Passing the argument
objProcess.StartInfo.Arguments = string.Format(strArgument);
try
{
objProcess.Start();
}
catch
{
throw new Exception("Batch file is not found for processing");
}
I am creating a process to do some working. But the process is starting a black window (like the cmd). I have tried to set the CreateNoWindow = true but that doesn't help.
Is there another way to disallow window creation?
Here is my code:
var worker1 = new Process();
worker1.EnableRaisingEvents = true;
worker1.StartInfo.CreateNoWindow = true;
worker1.StartInfo.ErrorDialog = true;
worker1.StartInfo.Arguments = job.BtcFilePath;
worker1.StartInfo.FileName = job.ExeFilePath;
worker1.Exited += new EventHandler(Worker1Exited);
Processors.Add(worker1);
Processors.Last().Start();
Processors.Last().PriorityClass = ProcessPriorityClass.BelowNormal;
*The Processors is a list of processors
BR
worker1.StartInfo.CreateNoWindow = true;
worker1.StartInfo.UseShellExecute = false;
This worked for me in a similar situation.
worker1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
This seems to be another option. (Did not test this, but found this while browsing http://www.dotnetperls.com/png)
I have some code that runs a cmd command in C# which works really well in a WinForm but when running this in a console app it doesn't work. I am a bit stuck as to why this is, I tried adding Windows.Forms as a reference and added the using to the code but this didn't work either. The only other thing I can think of is that because it is running as a console it can't run another console window on top of this?
Any help is appreciated.
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine("command to run");
The following code will perform any console command you want and output the console text in your current window, everything after while(true) is just as example:
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = false;
cmd.WindowStyle = ProcessWindowStyle.Normal;
Process console = Process.Start(cmd);
while(true)
console.StandardInput.WriteLine("pause");
If you don't want any console output then set CreateNoWindow to true. Also this code works inside a console application using System.Diagnostics
Good luck!