0

I am replacing many batch files, that do almost the exact same thing, with one WPF executable. I have the program written, but I am having troubles with my "console" like display.

Within the program I call an executable to perform a task. This executable sends it's output messages to the console. I am able to redirect those to my "console" like display with the following.

Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

p.StartInfo.FileName = MyExecutable;
p.StartInfo.Arguments = MyArguments;
p.Start();

while (!p.StandardOutput.EndOfStream)
{
   string _output = p.StandardOutput.ReadLine();

   // This is my display string ObservableCollection
   _displayString.Add(new DisplayData { _string = _output, _color = System.Windows.Media.Brushes.Black, _fontSize = 12 });

   // This is to redirect to a Console & what I want to get rid of 
   Console.WriteLine(_output);
}

p.WaitForExit();

When the executable is done running I use a MessageBox to ask the user if they want to run the executable again. The problem is that when the MessageBox is up the user is unable to scroll on my "console" like window to make an informative decision. To temporarily work around this I launch a console at start up, see this, and write the output stream from the process running the executable to the console so the user can scroll through the information and make a decision.

I am using a listbox to display a collection of textblocks, see this. I am not attached to anything for making the display, or MessageBox'es.

How can I make a "console" like display that will take a user input (MessageBox, direct input, etc...), but also allow them to scroll through the data before they make their decision?

EDIT: As to the comments I have been receiving I am not clear on what I am trying to accomplish. I have a batch file that runs a executable multiple times with different arguments per run. There is a lot of setup before and between the executable calls. I have created an executable to replace many flavors of a similar batch file with drop down menus for the user to change settings at run time. When the user likes their settings they click a "Start" button, and away it goes doing setups and prompting questions as it goes and then finally runs executable for the first time.

My issue is when the called executable, inside mine, is done running the user needs to decide if they want to run it again for different reasons. I need to prompt the user "Run again - 'Yes' or 'No'?", and that is where I am running into problems. A MessageBox doesn't allow me to scroll on my Progress Window. I have tried a Modeless dialog box, but with Show() the program continues on, and ShowDialog() is the same issue as the MessageBox.

Any suggestions on how to do this would be appreciated.

Community
  • 1
  • 1
Bluto
  • 166
  • 1
  • 15
  • Your question is not clear. You have not show your "problem code" example, but provide many useless info. If you want not to block your window - use modeless dialog boxes. – Maximus Feb 21 '13 at 18:34
  • Do you actually need a messagebox though? Can you just have your message at the bottom of the window with 2 buttons Yes and No? – failedprogramming Feb 21 '13 at 22:29
  • @Maximus I did try this, but with Show() it just continues, and with ShowDialog() it won't let me scroll. – Bluto Feb 22 '13 at 14:37
  • @failedprogramming I have thought of this, but haven't put a lot of thought into it. I am trying to think of a good way to skip around with a stop an wait. – Bluto Feb 22 '13 at 14:38
  • @failedprogramming Also I want the message to pop out at the user since the internally called executable can cause the user to day dream with how long it takes. – Bluto Feb 22 '13 at 15:18
  • Well that was a pain in the butt. Thank goodness I didn't delete the Dialog I tried Modeless. I used Show() on my Modeless Dialog and put actions in my code behind to invoke the next step in the sequence. Not pretty, and why I didn't put it as the answer. – Bluto Feb 22 '13 at 20:45
  • it sounds like your external program is running on some kind of loop and you are relying on showdialog method to block your UI thread, n wait for user input. I would honestly prefer not to use logic that works this way. I would prefer converting the external logic into the wpf program. But that may not be an option for you – failedprogramming Feb 23 '13 at 08:46
  • One possible solution would be to have your buttons and text input below the console control. You would set their visibility to hidden or collapsed while your external program is running. Once it finishes running, you can show the buttons. To block the ui thread, you can define a class Boolean variable, which would be true while waiting for user input. YOur wpf program can run an endless loop and sleep if variable is true. However, this will most likely also block user input, so you will have to move this logic to a second thread. A crappy solution at best... – failedprogramming Feb 23 '13 at 09:02
  • @failedprogramming I agree I have a crappy solution. I took my nice inline stream of code and broke it up in blocks. So when ever the user says 'Yes' or 'No' a different action is called based on enum flags. Your idea of Visble/Hidden buttons would give me a solution very similar to the one I came up with. Each button would have to invoke some function call, I used Actions for this with being in different classes. If you are thinking something else you could post a possible answer to show me more of what you mean. – Bluto Feb 25 '13 at 18:23

1 Answers1

0

You are in Windows, but trying to use DOS paradigm. Windows is event-based system! You need to write "event handlers" but not put all your code in one function.

However, there is a trick, which allows to show Modal (read "blocking your code") dialog in Modeless (read "not blocking your window"). Not sure how to implement this in WPF, but idea is to re-enable your window (which acts as parent for your dialog). You need to do this in your dialog event handler (WM_INITDIALOG equivalent?).

Also (in WinAPI) you may run dialog with NULL as parent window.

Maximus
  • 10,751
  • 8
  • 47
  • 65