Re: Display output of the CMD in real time inside TextBox
Posted: Wed Aug 08, 2018 11:03 am
Here is how to make a full async read work in a form.
SAPIEN Technologies Inc.
https://www.sapien.com/forums/
Faulting application name: scriptdriver64.exe, version: 1.4.48.0, time stamp: 0x5ae7594d
Faulting module name: KERNELBASE.dll, version: 10.0.17134.165, time stamp: 0xb0bb231d
Exception code: 0xe0434352
Fault offset: 0x000000000003a388
Faulting process id: 0x4dd0
Faulting application start time: 0x01d42f48f7837a64
Faulting application path: C:\Program Files (x86)\Common Files\SAPIEN Debugger 2018\scriptdriver64.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: bd3e6bcf-7744-4b97-b1a6-c5b9b50365ce
Faulting package full name:
Faulting package-relative application ID:
Code: Select all
$buttonSendInput_Click={
$process.StandardInput.WriteLine($textboxInput.Text)
}
This is what I've manage to create:
Code: Select all
$formAsyncPingerTest_Load = {
$pstart = 'SELECT * FROM Win32_ProcessStartTrace'
$pstop = 'SELECT * FROM Win32_ProcessStopTrace'
$action = {
$nevent = $Event.SourceEventArgs.NewEvent
if ($nevent.ProcessName -eq 'ping.exe' -and $Event.SourceIdentifier -eq 'Process.Stop') {
$global:processExited = $true
}
if ($nevent.ProcessName -eq 'nslookup.exe' -and $Event.SourceIdentifier -eq 'Process.Stop') {
$global:processExited = $true
}
}
Register-WmiEvent -Query $pstart -SourceIdentifier 'Process.Start' -Action $action
Register-WmiEvent -Query $pstop -SourceIdentifier 'Process.Stop' -Action $action
}
$global:process_OutputReceived = {
$global:processExited = $false
$textbox1.Lines += $_.Data
$textbox1.Select($textbox1.Text.Length, 0)
$textbox1.ScrollToCaret()
}
$global:process_Exited = {
$global:processExited = $true
Write-Host 'Exited'
Write-Host $global:processExited
}
$buttonPing_Click = {
'ping', 'ping' | % {
$script:process = New-Object System.Diagnostics.Process
$process.EnableRaisingEvents = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardInput = $true
$process.StartInfo.FileName = $_
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.CreateNoWindow = $true
$process.StartInfo.Arguments = '-n 5 google.com'
$process.add_OutputDataReceived($process_OutputReceived)
$process.add_Exited($process_Exited)
$process.SynchronizingObject = $formAsyncPingerTest
$global:processExited = $false
$process.Start() | Out-Null
$process.BeginOutputReadLine()
while ($global:processExited -eq $false) {
[System.Threading.Thread]::Sleep(1000)
Write-Host $global:processExited
}
}
}
$buttonStop_Click={
#TODO: Place custom script here
$process.CancelOutputRead()
$process.Kill()
$process.Dispose()
}
$formAsyncPingerTest_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
#TODO: Place custom script here
$process.CancelOutputRead()
$process.Kill()
$process.Dispose()
Get-EventSubscriber | Unregister-Event -Force
}
The c# static Task<int> RunProcessAsync code from you post is too complicated, maybe I will find powershell version of it but does it will suit my concept from above?The following can also be done in PowerShell: