Process.StandardOutput Özellik
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Uygulamanın metin çıkışını okumak için kullanılan bir akış alır.
public:
property System::IO::StreamReader ^ StandardOutput { System::IO::StreamReader ^ get(); };
public System.IO.StreamReader StandardOutput { get; }
[System.ComponentModel.Browsable(false)]
public System.IO.StreamReader StandardOutput { get; }
member this.StandardOutput : System.IO.StreamReader
[<System.ComponentModel.Browsable(false)>]
member this.StandardOutput : System.IO.StreamReader
Public ReadOnly Property StandardOutput As StreamReader
Özellik Değeri
StreamReader Uygulamanın standart çıkış akışını okumak için kullanılabilecek bir.
- Öznitelikler
Özel durumlar
Akış StandardOutput yeniden yönlendirme için tanımlanmamıştır; olarak ayarlandığından RedirectStandardOutput ve true olarak ayarlandığından UseShellExecuteemin olunfalse.
-veya-
Akışı StandardOutput ile BeginOutputReadLine()zaman uyumsuz okuma işlemleri için açıldı.
Örnekler
Aşağıdaki örnek ipconfig.exe komutunu çalıştırır ve standart çıktısını örneğin konsol penceresine yönlendirir.
using System;
using System.IO;
using System.Diagnostics;
class StandardOutputExample
{
public static void Main()
{
using (Process process = new Process())
{
process.StartInfo.FileName = "ipconfig.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
// Synchronously read the standard output of the spawned process.
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
// Write the redirected output to this application's window.
Console.WriteLine(output);
process.WaitForExit();
}
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadLine();
}
}
module ProcessStandardOutput
open System.Diagnostics
use proc = new Process()
proc.StartInfo.FileName <- "ipconfig.exe"
proc.StartInfo.UseShellExecute <- false
proc.StartInfo.RedirectStandardOutput <- true
proc.Start() |> ignore
// Synchronously read the standard output of the spawned process.
let reader = proc.StandardOutput
let output = reader.ReadToEnd()
// Write the redirected output to this application's window.
printfn $"{output}"
proc.WaitForExit()
printfn "\n\nPress any key to exit."
stdin.ReadLine() |> ignore
Imports System.IO
Imports System.Diagnostics
Module Module1
Sub Main()
Using process As New Process()
process.StartInfo.FileName = "ipconfig.exe"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.Start()
' Synchronously read the standard output of the spawned process.
Dim reader As StreamReader = process.StandardOutput
Dim output As String = reader.ReadToEnd()
Console.WriteLine(output)
process.WaitForExit()
End Using
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
Console.ReadLine()
End Sub
End Module
Açıklamalar
Bir Process metin standart akışına yazıldığında, bu metin normalde konsolda görüntülenir. Akışı yeniden yönlendirerek StandardOutput bir işlemin çıkışını işleyebilir veya gizleyebilirsiniz. Örneğin, metni filtreleyebilir, farklı biçimlendirebilir veya çıkışı hem konsola hem de belirlenmiş bir günlük dosyasına yazabilirsiniz.
Note
kullanmak StandardOutputiçin olarak ve ProcessStartInfo.UseShellExecutefalseolarak ayarlamanız ProcessStartInfo.RedirectStandardOutputtruegerekir. Aksi takdirde, akıştan StandardOutput okuma bir özel durum oluşturur.
Yeniden yönlendirilen StandardOutput akış zaman uyumlu veya zaman uyumsuz olarak okunabilir. , Readve ReadLine gibi ReadToEndyöntemler, işlemin çıkış akışında zaman uyumlu okuma işlemleri gerçekleştirir. İlişkili yazma işlemi akışına yazılana Process veya akışı kapatana kadar StandardOutput bu zaman uyumlu okuma işlemleri tamamlanmaz.
Buna karşılık, BeginOutputReadLine akışta StandardOutput zaman uyumsuz okuma işlemlerini başlatır. Bu yöntem, akış çıkışı için belirlenmiş bir olay işleyicisini etkinleştirir ve hemen çağırana döner ve akış çıkışı olay işleyicisine yönlendirilirken diğer işleri gerçekleştirebilir.
Zaman uyumlu okuma işlemleri, çağıranın akıştan StandardOutput okuması ile bu akışa yazma alt işlemi arasında bir bağımlılık oluşturur. Bu bağımlılıklar kilitlenme koşullarına neden olabilir. Çağıran bir alt işlemin yeniden yönlendirilen akışından okursa, alt öğeye bağımlı olur. Çağıran, alt öğe akışa yazana veya akışı kapatana kadar okuma işlemini bekler. Alt işlem yeniden yönlendirilen akışını doldurmak için yeterli veri yazdığında, üst öğeye bağımlıdır. Alt işlem, üst öğe tam akıştan okuyana veya akışı kapatana kadar bir sonraki yazma işleminde bekler. Çağıranın ve alt işlemin bir işlemi tamamlamak için birbirini beklemesi ve devam edememesi durumunda kilitlenme koşulu oluşur. Çağıran ile alt işlem arasındaki bağımlılıkları değerlendirerek kilitlenmeleri önleyebilirsiniz.
Bu bölümdeki son iki örnek, Startadlı bir yürütülebilir dosyayı başlatmak için yöntemini kullanır. Aşağıdaki örnek kaynak kodunu içerir.
using System;
public class Example3
{
public static void Main()
{
for (int ctr = 0; ctr < 500; ctr++)
Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}");
Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n");
}
}
// The example displays the following output:
// Line 1 of 500 written: 0,20%
// Line 2 of 500 written: 0,40%
// Line 3 of 500 written: 0,60%
// ...
// Line 498 of 500 written: 99,60%
// Line 499 of 500 written: 99,80%
// Line 500 of 500 written: 100,00%
//
// Successfully wrote 500 lines.
module Write500Lines
for i in 1. .. 500. do
printfn $"Line {i} of 500 written: {i/500.:P2}";
eprintfn "Successfully wrote 500 lines.";
// The example displays the following output:
// Line 1 of 500 written: 0,20%
// Line 2 of 500 written: 0,40%
// Line 3 of 500 written: 0,60%
// ...
// Line 498 of 500 written: 99,60%
// Line 499 of 500 written: 99,80%
// Line 500 of 500 written: 100,00%
// Successfully wrote 500 lines.
Imports System.IO
Public Module Example
Public Sub Main()
For ctr As Integer = 0 To 499
Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}")
Next
Console.Error.WriteLine($"{vbCrLf}Successfully wrote 500 lines.{vbCrLf}")
End Sub
End Module
' The example displays the following output:
' Line 1 of 500 written 0,20%
' Line 2 of 500 written: 0,40%
' Line 3 of 500 written: 0,60%
' ...
' Line 498 of 500 written: 99,60%
' Line 499 of 500 written: 99,80%
' Line 500 of 500 written: 100,00%
'
' Successfully wrote 500 lines.
Aşağıdaki örnekte, yeniden yönlendirilen bir akıştan okuma ve alt işlemin çıkmasını bekleme işlemleri gösterilmektedir. Örnek, önce p.StandardOutput.ReadToEndçağırarak p.WaitForExit kilitlenme koşulundan kaçınıyor. Üst işlem daha önce p.WaitForExit çağırırsa p.StandardOutput.ReadToEnd ve alt işlem yeniden yönlendirilen akışı doldurmak için yeterli metin yazarsa kilitlenme koşulu oluşabilir. Üst işlem, alt işlemin çıkması için süresiz olarak bekler. Alt işlem, üst işlemin tam StandardOutput akıştan okuması için süresiz olarak bekler.
using System;
using System.Diagnostics;
public class Example2
{
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
}
}
// The example displays the following output:
// Successfully wrote 500 lines.
//
// The last 50 characters in the output stream are:
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
module STDOutputSync
open System.Diagnostics
let p = new Process()
p.StartInfo.UseShellExecute <- false
p.StartInfo.RedirectStandardOutput <- true
p.StartInfo.FileName <- "Write500Lines.exe"
p.Start() |> ignore
// To avoid deadlocks, always read the output stream first and then wait.
let output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
printfn $"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"
// The example displays the following output:
// Successfully wrote 500 lines.
// The last 50 characters in the output stream are:
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
Imports System.Diagnostics'
Public Module Example
Public Sub Main()
Dim p As New Process()
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.FileName = "Write500Lines.exe"
p.Start()
' To avoid deadlocks, always read the output stream first and then wait.
Dim output As String = p.StandardOutput.ReadToEnd()
p.WaitForExit()
Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'")
End Sub
End Module
' The example displays the following output:
' Successfully wrote 500 lines.
'
' The last 50 characters in the output stream are:
' 'ritten: 99,80%
' Line 500 of 500 written: 100,00%
' '
Hem standart çıktıdan hem de standart hata akışlarından tüm metinleri okuduğunuzda benzer bir sorun oluşur. Aşağıdaki örnek her iki akışta da bir okuma işlemi gerçekleştirir. Akışta zaman uyumsuz okuma işlemleri gerçekleştirerek kilitlenme koşulunu StandardError önler. Üst işlem tarafından çağrılması p.StandardOutput.ReadToEndp.StandardError.ReadToEnd ve alt işlemin hata akışını doldurmak için yeterli metin yazması durumunda kilitlenme koşulu oluşur. Üst işlem, alt işlemin akışını kapatması StandardOutput için süresiz olarak bekler. Alt işlem, üst işlemin tam StandardError akıştan okuması için süresiz olarak bekler.
using System;
using System.Diagnostics;
public class Example
{
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string eOut = null;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{ eOut += e.Data; });
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
Console.WriteLine($"\nError stream: {eOut}");
}
}
// The example displays the following output:
// The last 50 characters in the output stream are:
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
//
// Error stream: Successfully wrote 500 lines.
module STDOutputAsync
open System.Diagnostics
let p = new Process()
p.StartInfo.UseShellExecute <- false
p.StartInfo.RedirectStandardOutput <- true
let mutable eOut = ""
p.StartInfo.RedirectStandardError <- true
p.ErrorDataReceived.AddHandler(DataReceivedEventHandler(fun sender e -> eOut <- eOut + e.Data))
p.StartInfo.FileName <- "Write500Lines.exe"
p.Start() |> ignore
// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine()
let output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
printfn $"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"
printfn $"\nError stream: {eOut}"
// The example displays the following output:
// The last 50 characters in the output stream are:
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
//
// Error stream: Successfully wrote 500 lines.
Imports System.Diagnostics
Public Module Example
Public Sub Main()
Dim p As New Process()
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
Dim eOut As String = Nothing
p.StartInfo.RedirectStandardError = True
AddHandler p.ErrorDataReceived, Sub(sender, e) eOut += e.Data
p.StartInfo.FileName = "Write500Lines.exe"
p.Start()
' To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine()
Dim output As String = p.StandardOutput.ReadToEnd()
p.WaitForExit()
Console.WriteLine($"The last 50 characters in the output stream are:{vbCrLf}'{output.Substring(output.Length - 50)}'")
Console.WriteLine($"{vbCrLf}Error stream: {eOut}")
End Sub
End Module
' The example displays the following output:
' The last 50 characters in the output stream are:
' 'ritten: 99,80%
' Line 500 of 500 written: 100,00%
' '
'
' Error stream: Successfully wrote 500 lines.
Bu bağımlılıkları ve kilitlenme potansiyellerini önlemek için zaman uyumsuz okuma işlemlerini kullanabilirsiniz. Alternatif olarak, iki iş parçacığı oluşturup her akışın çıkışını ayrı bir iş parçacığında okuyarak kilitlenme durumundan kaçınabilirsiniz.
Note
Yeniden yönlendirilen bir akışta zaman uyumsuz ve zaman uyumlu okuma işlemlerini karıştıramazsınız. Bir öğesinin yeniden yönlendirilen akışı Process zaman uyumsuz veya zaman uyumlu modda açıldıktan sonra, bu akıştaki diğer tüm okuma işlemleri aynı modda olmalıdır. Örneğin, akışta BeginOutputReadLine çağrısıyla ReadLine (veya tam tersi) takip StandardOutput etmeyin. Ancak, farklı modlarda iki farklı akışı okuyabilirsiniz. Örneğin, akışı çağırabilir BeginOutputReadLine ve ardından çağırabilirsiniz ReadLineStandardError .