Read Output From A Running Process Realtime (Command Prompt) - process

I have been researching for an answer for my question for a long while using Google and changing what words I use in-case I find my answer, I have had no luck.
What I want is to the read the output from a process in real-time, not when it finishes the command it has been given. I have a command that I use which doesn't end unless the user ends it manually, closes the form or an error occurs. So every timer interval I would like to read the output from the command prompt (process).
I use background workers to start the process so it doesn't interfere with the form if that is any help.
Thanks in advance.

Related

How to run DOS/CMD/Command Prompt commands from VB.NET and want to hold its terminal output for few seconds?

How to run DOS/CMD/Command Prompt commands from VB.NET and want to hold its terminal output for few seconds. After that it should be close automatically.
Depending on your goal, there could be multiple solutions to this request.
This SO post details how you can run a command from cmd.exe, passing arguments.
How to run DOS/CMD/Command Prompt commands from VB.NET?
Another idea, depending on the goal, would be to save the cmd as a batch file or similar, where you could have a pause or timed wait before the cmd window closes.
Then you could call the batch file from vb.net
I don't want to dive too deep into how each of these would be done without knowing more about the goal at hand.

Delete a file even if it is running

I need to remove a file even if it is used by a running process.
Firstly, of course the process needs to be shut down, and after that the file should be deleted, if it exists.
I'm using the following code:
Sample:
Dim Processes() As Process = Process.GetProcessesByName("test")
For Each Process As Process In Processes
Process.Kill()
Next
My.Computer.FileSystem.DeleteFile(C:\ProgramFiles\Test\test.exe)
I tried the code above, it does not work, file is still running and also it is not removed! Can you please provide a reliable solution to this issue?
Thanks.
It may take a little while for the after-effects of Process.Kill() to complete.
If you add a delay between the Kill command and the File.Delete, such as Threading.Thread.Sleep(2000), it may be sufficient for the latter to complete.
A more robust solution might involve repeatedly attempting to delete the file, up to some reasonable number of tries, with a small time delay between attempts.

How to read keyboard inputs in Octave while running loop?

Problem:
I'm running a script that includes an infinite loop and I would like to exit from this loop with user input. I don't want to use the standard "input" function because that pauses the execution of the loop while it waits for the user inputs. I want that the program keeps looping all the time (until some certain keyboard input is given). I don't want to exit from the loop with ctrl+c either because then the program shut down procedures that are located after the loop, are not executed.
Question:
In octave, when something is typed to the command window during execution of a script nothing is shown in the command window until the script has ended. From this it is clear that keyboard inputs that are given during the execution of a script are stored somewhere (is this right?). And now the big guestion is where? And how can I access this data?
I'm running Octave 4.0.0 in Win7
p.s. also other suggestions for stopping the loop are welcome
Use kbhit:
while (1)
if (kbhit (1) == 'x')
break
endif
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
Or if you want to exit with ctrl-c and finalize your script use a unwind_protect, unwind_ptrotect_cleanup block
unwind_protect
while (1)
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
unwind_protect_cleanup
disp ("doing my cleanup");
end_unwind_protect
In octave, when something is typed to the command window during execution of a script nothing is shown in the command window until the script has ended. From this it is clear that keyboard inputs that are given during the execution of a script are stored somewhere (is this right?). And now the big guestion is where? And how can I access this data?
This is called buffering and is a very common behaviour. The principle is simple. Instead of writing everything as it's ready, your system will keep it on a buffer and only write it when told to do so, or when the buffer is full. Many disks operations work like this, for example, when you copy a few small files into a USB stick (dependent on the mount options). Once the buffer is full, or when you click to eject the USB, stick, the system will then actually perform the write.
If you read the section Paging Screen Output of the Octave manual you will see:
Normally, no output is displayed by the pager until just before Octave is ready to print the top level prompt, or read from the standard input (for example, by using the fscanf or scanf functions). This means that there may be some delay before any output appears on your screen if you have asked Octave to perform a significant amount of work with a single command statement. The function fflush may be used to force output to be sent to the pager (or any other stream) immediately.
Alternatively, you can turn off this buffering with page_output_immediately():
Query or set the internal variable that controls whether Octave sends output to the pager as soon as it is available.
Otherwise, Octave buffers its output and waits until just before the prompt is printed to flush it to the pager.

Is there a way in vb.net to make process start closing my program?

My program checks if there is a new version of itself. If yes it would exit and start an updater that replaces it and then restarts.
My problem is that I haven't found any info on how to make process start right after closing the actual program.
Any suggestions?
Thanks in advance
I intended to add a comment, but I'm too low in points here. The updater itself should probably contain a check to determine whether your application is running an instance, and it should contain a timeout loop that performs this check and factor the timeout following it's startup state. That way you can awaken it, and close your application. The updater should just determine your application is not running, compare versions perform the intended update operation.
a possible solution would also be to create a task via tash sceduler or cron job, starting an out of process application, like CMD.exe.. which brings me to my original comment-question: in regards to what Operating System(s) and Platform(s) is your program intended for?

Kill process after launching with AuthorizationExecuteWithPrivileges

If I launched a shell script using AuthorizationExecuteWithPrivileges what would be the easiest way to kill the script and any other processes that it spawned.
Thanks
It's running as root, so you can't kill it from a regular-user process. You're going to have to ask it nicely to exit on its own.
Apple has sample code that uses stdout to pass the PID back to the caller.
Use the communications pipe that AuthorizationExecuteWithPrivileges() returns by reference in its last argument, FILE **communicationPipe, to send a message to the child process that asks it to take itself and its descendants out. It can then kill itself and all its descendants using kill(0, SIGINT), or, if more drastic measures are required, SIGKILL.
The message you use can be as simple as closing the file while the child waits for the file to close; at that point, it knows you're done talking to it and it's time to take itself out.
There are some caveats about the descendants that will actually receive this message, for which see the kill(2) manpage. The caveats mostly won't matter so long as the process you started via AEWP hasn't dropped privileges, though one implicit issue is that this approach won't work if any child processes have put themselves in a new process group.