0

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();
        }
pradeepradyumna
  • 952
  • 13
  • 34
  • You could hide all the windows, but then you'd probably want to open another window so at least something is showing? – Derek Aug 25 '17 at 10:56
  • Or you could do it like a batch file, but then all the actions would be running one at a time instead of at the same time. – Derek Aug 25 '17 at 10:57
  • @Derek yes, I want just one window to be open. So that I won't be given multiple pops and grabbing the focus making my computer unusable until all the processes completes. – pradeepradyumna Aug 25 '17 at 11:00
  • If you detach above snippet from your View code, then you can make a different View, for example a UserControl that shows the Data of one process and use one or more of those in one single Window. – Fildor Aug 25 '17 at 11:04
  • 1
    pInfo.CreateNoWindow = true; – Derek Aug 25 '17 at 11:09
  • Read about inter-process communications. Assuming that `executable` is your own software, instead of running a new process you can pass information (using IPC) into existing running process. Related subject: how to ensure only single instance of program is running (e.g. using mutex). One easy possibility is to utilize `FileSystemWatcher`, where to pass parameters you simply create a file in the folder, which is monitored by `executable`. – Sinatr Aug 25 '17 at 12:43
  • It's not clear what you are trying to do. Do you want all ten processes to run concurrently? If so, you can't run them all in a single window. Do you really want all the windows visible? If not, one option is to simply hide the window. Do you still want to be able to see output from the processes? If so, one option is to redirect output and display it in your Winforms program. As stated, it's not even clear what you want, and the question has far too many possible answers. – Peter Duniho Aug 28 '17 at 05:12

1 Answers1

0

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;
Derek
  • 7,615
  • 5
  • 33
  • 58
  • Thank you I have tried this, but it will turn off all the windows. My requirement is like, if I can just open one command window for all the actions :) – pradeepradyumna Aug 25 '17 at 11:21
  • Can you run all the actions one after another, or do they have to run at the same time? – Derek Aug 25 '17 at 11:35
  • You could also look at this: https://stackoverflow.com/questions/649634/how-do-i-run-a-bat-file-in-the-background-from-another-bat-file It looks like "start /b" can be used in a batch file to start another process without opening a new window. – Derek Aug 25 '17 at 11:38
  • Yes I can run actions one after another. By the way the actions here are git Fetch/ Pull for selected repositories. So I can do it one by one. – pradeepradyumna Aug 25 '17 at 11:41
  • I have never used batch file, so I wanted to know is there a way that I could append these commands and run it all for once. I'm not sure if that is even possible. Just a guess. – pradeepradyumna Aug 25 '17 at 11:55
  • One easy way is to use cmd /c with ampersand. https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-one-line-in-windows-cmd/8055430 Then you don't need to make a batch file – Derek Aug 25 '17 at 11:58
  • 1
    A simple way to think of a batch file is a list of commands to run one after another from a command prompt. You could save a .bat file with all your commands in order and then run that .bat file. You would test your .bat file on it's own from the command prompt. Sometimes you can have your program save a batch file to disk ( if you need to build up the command strings at run time ) and then run that file. – Derek Aug 25 '17 at 12:01