0

I making an application where within a function another application is launched with parameters using Processs.start()

The streams are redirected to a richtextbox which prints everything the console does, whilst the console window is hidden. For the purposes of debugging, the console is visible (and blank).

Here are the snippets of code in question:

Delegate Sub myappthing_boxDelg(myappthing_text As String)
Public myappthing_delegate As myappthing_boxDelg = New myappthing_boxDelg(AddressOf myappthing)


Public Sub myappthing(myappthing_text As String)

    richtextbox.Text += myappthing_text & Environment.NewLine
    richtextbox.SelectionStart = richtextbox.Text.Length
    richtextbox.ScrollToCaret()
    richtextbox.Text = richtextbox.Text.Replace("[0m", "")
End Sub

The above is what is used to display what the console says in the richtextbox, as well as removing any formatting errors in the last line.

    Public Sub myappthing_procDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    If Me.InvokeRequired = True Then
        Me.Invoke(myappthing_delegate, e.Data)
    Else
        myappthing(e.Data)
    End If
End Sub

Below is what I use to end the process, it does not need to be a clean ending so the process is just killed, and handlers removed so it can be relaunched within the same instance of my application.

Public Sub kill_myappthing()
    If myappthing_proc_state = True Then
        myappthing_proc.Kill()
        RemoveHandler myappthing_proc.ErrorDataReceived, AddressOf myappthing_procDataReceived
        RemoveHandler myappthing_proc.OutputDataReceived, AddressOf myappthing_procDataReceived
        myappthing_proc.CancelErrorRead()
        myappthing_proc.CancelOutputRead()
        myappthing_proc_state = False
        lbl_status.Text = "Status: Stopped"
    End If
End Sub

Below is where I think the problem lies.

 Private Sub myappthing()
        myappthing_proc.StartInfo.FileName = ".\backend\myappthingexecutable"
    Dim args As String = "-argument"
    myappthing_proc.StartInfo.Arguments = ("args")
    myappthing_proc.StartInfo.WorkingDirectory = ".\backend\"
    myappthing_proc.StartInfo.RedirectStandardError = True
    myappthing_proc.StartInfo.RedirectStandardOutput = True
    myappthing_proc.EnableRaisingEvents = True
    myappthing_proc.StartInfo.UseShellExecute = False
    myappthing_proc.StartInfo.CreateNoWindow = False
    Application.DoEvents()
    AddHandler myappthing_proc.ErrorDataReceived, AddressOf myappthing_procDataReceived
    AddHandler myappthing_proc.OutputDataReceived, AddressOf myappthing_procDataReceived
    myappthing_proc.Start()
    myappthing_proc.BeginErrorReadLine()
    myappthing_proc.BeginOutputReadLine()
    myappthing_proc_state = True
 End Sub

When i run the application and have these functions run the executable it does not print anything in the console window that pops up or in the richtextbox, however when running the executable using a batch file everything prints in the console window perfectly well. I am at a loss of what to do.

Any help or a point in the right direction would be much appreciated. Thank you!

In the code I've provided, the executable is found and is technically run (its process can be seen in task manager), but its console output is blank and it does not actually do anything. One idea I have is that maybe the executable I'm trying to run is itself spawning a child process/second console window which is the one I am trying to get the output from, but I don't know how to go about redirecting the streams from a process that I myself haven't started.

  • I think you might have a conflict with the working directory and the filename. Try removing the working directory from the filename. – the_lotus Jul 09 '18 at 13:48
  • @the_lotus Doing that causes a 'system cannot find the file specified' error. In the code I've provided, the executable is found and is technically run (its process can be seen in task manager), but its console output is blank and it does not actually do anything. One idea I have is that maybe the executable I'm trying to run is itself spawning a child process/second console window which is the one I am trying to get the output from, but I don't know how to go about redirecting the streams from a process that I myself haven't started. – Parth Kataria Jul 09 '18 at 14:03
  • Have you tried running your process without any output redirection to see if this part works? – the_lotus Jul 09 '18 at 14:16
  • I haven't tested your code, but try new-ing the Process when you initialized it, the same for its ProcessStartInfo and specifing a SynchronizingObject (setting it to the Form where the RichTextBox is). Something like `MyProcess = New Process() With {.StartInfo = MyStartInfo, .EnableRaisingEvents = True, .SynchronizingObject = Me}`. Also, your `myappthing_proc.StartInfo.Arguments = ("args")` should be `myappthing_proc.StartInfo.Arguments = args` – Jimi Jul 09 '18 at 14:27
  • @Jimi thank you for your help however it still does not work. What i have discovered is when running the application using a batch file is that three child apps are present, windows command processor, the nameofexeimtryingtorun.exe, and console window host. However when running using my application the windows command processor is absent, and only nameofexeimtryingtorun.exe and console window host are present. Do you think this might be related to the issue? – Parth Kataria Jul 09 '18 at 14:38
  • Note that when redirecting the standard output stream the application will no longer print anything to the console window because, well, _you redirected the output elsewhere_ (in this case to your app). – Visual Vincent Jul 09 '18 at 14:43
  • Which version of Visual Studio are you using? – Visual Vincent Jul 09 '18 at 14:48
  • What @Visual Vincent said, you're using `.UseShellExecute = False` for the redirection, so no suprise there. The output could be buffered, but I have no idea how that app works. See whether it actually needs those `-arguments`, because, as of now, you're not passing them. – Jimi Jul 09 '18 at 14:51
  • @Visual Vincent Visual Studio 2017, and the application is built in .NET 4.5 – Parth Kataria Jul 09 '18 at 14:55
  • @Jimi I had made a typo when posting the code onto stack overflow. The actual line reads `myappthing_proc.StartInfo.Arguments = ("--noUAC" & args)` – Parth Kataria Jul 09 '18 at 14:57
  • @VisualVincent you were correct in the redirectstandardoutput and redirecterroroutput being set to false as well as the beginoutputreadline and beginerrorreadline being removed resulted in the console window being occupied with code. This would suggest that the problem is in the redirection itself, and not in the application I am trying to start. – Parth Kataria Jul 09 '18 at 15:01
  • Make a test. Try executing `tracert.exe` passing as arguments an IP address. See if you can catch the output correctly with the code you have. – Jimi Jul 09 '18 at 15:02
  • @Jimi I have done so and as I do not know how to use tracert the traceroute itself did not take place, however it did run and did display that the parameter (an ip on the same network) was an unknown parameter, which means that the arguments are being passed onto whatever application is being run. I would also like to clarify that when I run the application manually using a batch file, ram and cpu usage increases as it is supposed to, yet when running using my original code while redirecting the outputs, cpu usage hovers near 1% and little ram is used, suggesting that nothing is happening. – Parth Kataria Jul 09 '18 at 15:13
  • Yes, but, did you get the output, error or not, as text in your RichTextBox control? – Jimi Jul 09 '18 at 15:31
  • @Jimi I did not get it in the richtextbox control while redirecting the streams no, I could only see the output by not redirecting the streams and viewing it in the console window with createnowindow set to false – Parth Kataria Jul 09 '18 at 15:32
  • You have more than one problem. Your Process is not disposed the right way, so its EventHandlers are not disposed either. You probably have more existing handlers that you know of. You have to `New` the Process and then dispose of it when killing/closing it. Then (probably), the arguments are not passed as intended (since the external program apparently is not run). I'll test your code when I have some free time. – Jimi Jul 09 '18 at 15:48
  • In response to the VS version, try changing the `Invoke` line in `myappthing_procDataReceived` to: `Me.Invoke(Sub() myappthing(e.Data))` and see what happens. – Visual Vincent Jul 09 '18 at 15:53
  • @Jimi thanks, I had thought that removing the handlers would be enough as the disposing of the process would cause issues when trying to run the executable again within the same instance of my app running, but I will find a way to dispose of the process whilst still retaining this functionality. – Parth Kataria Jul 09 '18 at 15:54
  • @VisualVincent that causes a too many arguments error as myappthing is the name of the function, I instead tried it with myappthing_delegate and whilst the code ran, it made no difference. – Parth Kataria Jul 09 '18 at 15:58
  • I just remember that I posted something like this in VB.Net. You can see it [here](https://stackoverflow.com/questions/48069500/using-await-in-backgroundworker-causes-workercomplete-event-to-be-raised?answertab=active#tab-top). Maybe it can give you some hints. – Jimi Jul 09 '18 at 16:04
  • @Jimi I have discovered that by removing critical configuration files from the application I'm trying to run, it is able to spit out error codes within the richtextbox. This leads me to believe when the application is operating properly, once the integrity checks are passed it creates a child process to actually do its task, and this child processes stream is not outputted to the richtextbox, although this doesn't explain why a second console window does not appear or why the application does not function when being launched using my code. – Parth Kataria Jul 09 '18 at 16:36
  • I tested your code. There are some things to change to make it work as expected. Keep in mind that I can't test your console application, so I focused on the functionality of the code you presented. Here is a [PasteBin repository code](https://pastebin.com/v9FYpwjw) of a Form Designer + Code that shows how a Console Output is redirected to a RichTextBox control in real time. It should require only a few changes to adapt it. Take care of how you define the Working directory and the Console app file path and how you pass the arguments. Handling the Exited event is required here. – Jimi Jul 10 '18 at 00:45
  • @Jimi thank you for your code, whilst it caused some errors at first regarding finding the executable, I was able to solve it by combining the startup path and app directory: `MyStartInfo.FileName = Path.Combine(Application.StartupPath, "backend/myappthing.exe")` This allowed errors configuring the 'myappthing' to show in the richtextbox in real time but once the application started to run properly it would only print the contents of the console in the richtextbox after 'myappthing' was killed. I am unsure as to what is causing this behaviour and don't know how to progress further. – Parth Kataria Jul 11 '18 at 10:27
  • You forgot to mention if the code I posted was working as expected when you tested it. -- Do you have the code of this console program? If so, post the relevant section. Also, update your question with the code you're using now (or post another question). – Jimi Jul 11 '18 at 14:42
  • @Jimi your code worked as normal, but when I replaced my existing code with yours and made the necessary changes, it stopped working. I believe this is an issue with the application I'm trying to run rather than the code itself. – Parth Kataria Jul 12 '18 at 14:38
  • That is quite probable. Now you're sure enough of what is the source of the problem. If you have access to that code, post the relevant parts in another question, if you can't find the reason why it's output is buffered (unless you inserted `Process.WaitForExit`. In this case that behaviour is normal). – Jimi Jul 12 '18 at 14:59

0 Answers0