Process.CancelErrorRead メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Process.CancelErrorRead メソッドの意味・解説 

Process.CancelErrorRead メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

アプリケーションリダイレクトされた StandardError ストリームで、非同期読み取り操作キャンセルします

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

<ComVisibleAttribute(False)> _
Public Sub CancelErrorRead
Dim instance As Process

instance.CancelErrorRead
[ComVisibleAttribute(false)] 
public void CancelErrorRead ()
[ComVisibleAttribute(false)] 
public:
void CancelErrorRead ()
/** @attribute ComVisibleAttribute(false) */ 
public void CancelErrorRead ()
ComVisibleAttribute(false) 
public function CancelErrorRead ()
例外例外
例外種類条件

InvalidOperationException

StandardError ストリームでは、非同期読み取り操作有効になっていません。

解説解説

BeginErrorReadLine は StandardError ストリーム非同期読み取り操作開始しCancelErrorRead非同期読み取り操作終了します

キャンセル後に非同期読み取り操作再開するには、BeginErrorReadLine再度呼び出します。

CancelErrorRead呼び出すと、StandardError対す進行中読み取り操作がすべて完了しイベント ハンドラ無効になります以降StandardErrorリダイレクトされた出力はすべてバッファ保存されます。イベント ハンドラ再度有効化するために BeginErrorReadLine呼び出すと、保存され出力イベント ハンドラ送られ非同期読み取り操作再開されます。非同期読み取り操作再開する前にイベント ハンドラ変更する場合は、新しイベント ハンドラ追加する前に既存イベント ハンドラ削除する必要があります

    // At this point the DataReceivedEventHandler(ErrorHandler1) 
    // has executed a CancelErrorRead.

    // Remove the prior event handler.
    process.ErrorDataReceived -= 
        new DataReceivedEventHandler(ErrorHandler1);

    // Register a new event handler.
    process.ErrorDataReceived += 
        new DataReceivedEventHandler(ErrorHandler2);

    // Call the corresponding BeginErrorReadLine.
    process.BeginErrorReadLine();
メモメモ

リダイレクトされた StandardError ストリームで、非同期読み取り操作同期読み取り操作混在させることはできません。Processリダイレクトされたストリーム非同期モードまたは同期モード開いた後は、それ以降、そのストリーム対すすべての読み取り操作は同じモードで行う必要がありますStandardError での非同期読み取り操作キャンセルした後で、そのストリームから再度読み取る場合は、BeginErrorReadLine使用して非同期読み取り操作再開する必要がありますCancelErrorRead の後に、ReadReadLine、ReadToEnd など、StandardError同期読み取りメソッド呼び出し続けないください

使用例使用例

ユーザー指定した引数使用して nmake コマンド開始する例を次に示しますエラー ストリームおよび出力ストリーム非同期読み取られます。収集されたテキスト行はコンソール表示されログ ファイル書き込まれます。コマンド出力指定した行数超えた場合非同期読み取り操作キャンセルされます。

' Define the namespaces used by this sample.
Imports System
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Imports System.Threading
Imports System.ComponentModel
Imports Microsoft.VisualBasic


Class ProcessNMakeStreamRedirection

   ' Define static variables shared by class methods.
   Private Shared buildLogStream As
 StreamWriter = Nothing
   Private Shared logMutex As
 Mutex = New Mutex()
   Private Shared maxLogLines As
 Integer = 25
   Private Shared currentLogLines As
 Integer = 0
   
   
   Public Shared Sub RedirectNMakeCommandStreams()
      Dim nmakeArguments As String
 = Nothing
      Dim nmakeProcess As Process
      
      ' Get the input nmake command-line arguments.
      Console.WriteLine("Enter the NMake command line arguments"
 + _
          " (@commandfile or /f makefile, etc):")
      Dim inputText As String
 = Console.ReadLine()
      If Not String.IsNullOrEmpty(inputText)
 Then
         nmakeArguments = inputText
      End If
      
      Console.WriteLine("Enter max line limit for log file (default
 is 25):")
      inputText = Console.ReadLine()
      If Not String.IsNullOrEmpty(inputText)
 Then
         If Not Int32.TryParse(inputText, maxLogLines)
 Then
            maxLogLines = 25
         End If
      End If
      Console.WriteLine("Output beyond {0} lines will be ignored.",
 _
          maxLogLines)
      
      ' Initialize the process and its StartInfo properties.
      nmakeProcess = New Process()
      nmakeProcess.StartInfo.FileName = "NMake.exe"
      
      ' Build the nmake command argument list.
      If Not String.IsNullOrEmpty(nmakeArguments)
 Then
         nmakeProcess.StartInfo.Arguments = nmakeArguments
      End If
      
      ' Set UseShellExecute to false for redirection.
      nmakeProcess.StartInfo.UseShellExecute = False
      
      ' Redirect the standard output of the nmake command.  
      ' Read the stream asynchronously using an event handler.
      nmakeProcess.StartInfo.RedirectStandardOutput = True
      AddHandler nmakeProcess.OutputDataReceived, _
                AddressOf NMakeOutputDataHandler
      
      ' Redirect the error output of the nmake command. 
      nmakeProcess.StartInfo.RedirectStandardError = True
      AddHandler nmakeProcess.ErrorDataReceived, _
                AddressOf NMakeErrorDataHandler

      logMutex.WaitOne()

      currentLogLines = 0
    
      ' Write a header to the log file.
      Const buildLogFile As String
 = "NmakeCmd.Txt"
      Try 
          buildLogStream = new StreamWriter(buildLogFile, true)
      Catch e As Exception
          Console.WriteLine("Could not open output file {0}",
 buildLogFile)
          Console.WriteLine("Exception = {0}", e.ToString())
          Console.WriteLine(e.Message)

          buildLogStream = Nothing
      End Try

      If Not buildLogStream Is
 Nothing Then
               
          Console.WriteLine("Nmake output logged to {0}",
 _
              buildLogFile)
    
          buildLogStream.WriteLine()
          buildLogStream.WriteLine(DateTime.Now.ToString())
          
          If Not String.IsNullOrEmpty(nmakeArguments)
 Then
              buildLogStream.Write("Command line = NMake {0}",
 _
                        nmakeArguments)
          Else 
              buildLogStream.Write("Command line = Nmake")
          End If
          
          buildLogStream.WriteLine()
          buildLogStream.Flush()
            
          logMutex.ReleaseMutex()
      
           ' Start the process.
           Console.WriteLine()
           Console.WriteLine("\nStarting Nmake command...")
           Console.WriteLine()
           nmakeProcess.Start()

           ' Start the asynchronous read of the error stream.
           nmakeProcess.BeginErrorReadLine()

           ' Start the asynchronous read of the output stream.
           nmakeProcess.BeginOutputReadLine()
    
           ' Let the nmake command run, collecting the output.
           nmakeProcess.WaitForExit()

           nmakeProcess.Close()
           buildLogStream.Close()
       End If
   End Sub 
   
    Private Shared Sub NMakeOutputDataHandler(sendingProcess
 As Object, _
       outLine As DataReceivedEventArgs)

        ' Collect the output, displaying it to the screen and 
        ' logging it to the output file.  Cancel the read
        ' operation when the maximum line limit is reached.

        If Not String.IsNullOrEmpty(outLine.Data)
 Then
            logMutex.WaitOne()

            currentLogLines = currentLogLines + 1
            If currentLogLines > maxLogLines Then
                
                ' Display the line to the console.
                ' Skip writing the line to the log file.
                Console.WriteLine("StdOut: {0}", outLine.Data)
            Else If currentLogLines = maxLogLines
 Then
                
                LogToFile("StdOut", "<Max
 build log limit reached!>", _
                    true)
    
                ' Stop reading the output streams.
                Dim p As Process = sendingProcess
 
                If Not (p Is
 Nothing) Then
                    p.CancelOutputRead()
                    p.CancelErrorRead()
                End If
            Else 
                ' Write the line to the log file.
                LogToFile("StdOut", outLine.Data,
 true)
            End If

            logMutex.ReleaseMutex()
        End If
 
    End Sub 
   
   Private Shared Sub NMakeErrorDataHandler(sendingProcess
 As Object, _
        errLine As DataReceivedEventArgs)

      ' Collect the error output, displaying it to the screen and 
      ' logging it to the output file.  Cancel the error output
      ' read operation when the maximum line limit is reached.

        If Not String.IsNullOrEmpty(errLine.Data)
 Then
            logMutex.WaitOne()

            currentLogLines = currentLogLines + 1
            If currentLogLines > maxLogLines Then
                
                ' Display the line to the console.
                ' Skip writing the line to the log file.
                Console.WriteLine("StdErr: {0}", errLine.Data)
            Else If currentLogLines = maxLogLines
 Then
                
                LogToFile("StdErr", "<Max
 build log limit reached!>", _
                    true)
    
                ' Stop reading the output streams.
                Dim p As Process = sendingProcess
 
                If Not (p Is
 Nothing) Then
                    p.CancelOutputRead()
                    p.CancelErrorRead()
                End If
            Else 
                ' Write the line to the log file.
                LogToFile("StdErr", errLine.Data,
 true)
            End If

            logMutex.ReleaseMutex()
        End If
 
    End Sub
   
    Private Shared Sub LogToFile(logPrefix
 As String, _
                                logText As String,
 _
                                echoToConsole As String)

        ' Write the specified line to the log file stream.
        Dim logString As StringBuilder = New
 StringBuilder()

        If Not String.IsNullOrEmpty(logPrefix)
 Then
                logString.AppendFormat("{0}> ",
 logPrefix)
        End If

        If Not String.IsNullOrEmpty(logText)
 Then
            logString.Append(logText)
        End If

        If Not buildLogStream Is
 Nothing Then
        
            buildLogStream.WriteLine("[{0}] {1}",
 _
                DateTime.Now.ToString(), logString.ToString())
            buildLogStream.Flush()
         End If
            
         If echoToConsole Then
            Console.WriteLine(logString.ToString())
         End If
  
    End Sub 
End Class 
namespace ProcessAsyncStreamSamples
{
    class ProcessNMakeStreamRedirection
    {
        // Define static variables shared by class methods.
        private static StreamWriter buildLogStream
 =null;
        private static Mutex logMutex = new
 Mutex();
        private static int
 maxLogLines = 25;
        private static int
 currentLogLines = 0;

        public static void
 RedirectNMakeCommandStreams()
        {
            String nmakeArguments = null;
            Process nmakeProcess;

            // Get the input nmake command-line arguments.
            Console.WriteLine("Enter the NMake command line arguments "
 +
                "(@commandfile or /f makefile, etc):");
            String inputText = Console.ReadLine();
            if (!String.IsNullOrEmpty(inputText))
            {
                nmakeArguments = inputText;
            }
               
            Console.WriteLine("Enter max line limit for log
 file (default is 25):");
            inputText = Console.ReadLine();
            if (!String.IsNullOrEmpty(inputText))
            {
                if (!Int32.TryParse(inputText, out maxLogLines))
                {
                    maxLogLines = 25;
                }
            }
            Console.WriteLine("Output beyond {0} lines will be ignored."
,
                maxLogLines);

            // Initialize the process and its StartInfo properties.
            nmakeProcess = new Process();
            nmakeProcess.StartInfo.FileName = "NMake.exe";
            
            // Build the nmake command argument list.
            if (!String.IsNullOrEmpty(nmakeArguments))
            {
                nmakeProcess.StartInfo.Arguments = nmakeArguments;
            }

            // Set UseShellExecute to false for redirection.
            nmakeProcess.StartInfo.UseShellExecute = false;

            // Redirect the standard output of the nmake command.  
            // Read the stream asynchronously using an event handler.
            nmakeProcess.StartInfo.RedirectStandardOutput = true;
            nmakeProcess.OutputDataReceived += new DataReceivedEventHandler(NMakeOutputDataHandler);
   
            // Redirect the error output of the nmake command. 
            nmakeProcess.StartInfo.RedirectStandardError = true;
            nmakeProcess.ErrorDataReceived += new DataReceivedEventHandler(NMakeErrorDataHandler);

            logMutex.WaitOne();

            currentLogLines = 0;

            // Write a header to the log file.
            const String buildLogFile = "NmakeCmd.Txt";
            try 
            {
                buildLogStream = new StreamWriter(buildLogFile,
 true);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not open output file {0}", buildLogFile);
                Console.WriteLine("Exception = {0}", e.ToString());
                Console.WriteLine(e.Message);

                buildLogStream = null;
            }

            if (buildLogStream != null)
            {   
                Console.WriteLine("Nmake output logged to {0}", 
                    buildLogFile);
    
                buildLogStream.WriteLine();
                buildLogStream.WriteLine(DateTime.Now.ToString());
                if (!String.IsNullOrEmpty(nmakeArguments))
                {
                    buildLogStream.Write("Command line = NMake {0}",
                        nmakeArguments);
                }
                else 
                {
                    buildLogStream.Write("Command line = Nmake");
                }
                buildLogStream.WriteLine();
                buildLogStream.Flush();
            
                logMutex.ReleaseMutex();

                // Start the process.
                Console.WriteLine();
                Console.WriteLine("\nStarting Nmake command...");
                Console.WriteLine();
                nmakeProcess.Start();

                // Start the asynchronous read of the error stream.
                nmakeProcess.BeginErrorReadLine();

                // Start the asynchronous read of the output stream.
                nmakeProcess.BeginOutputReadLine();
    
                // Let the nmake command run, collecting the output.
                nmakeProcess.WaitForExit();

                nmakeProcess.Close();
                buildLogStream.Close();
            }
        }

        private static void
 NMakeOutputDataHandler(object sendingProcess, 
            DataReceivedEventArgs outLine)
        {
            // Collect the output, displaying it to the screen and 
            // logging it to the output file.  Cancel the read
            // operation when the maximum line limit is reached.

            if (!String.IsNullOrEmpty(outLine.Data))
            {
                logMutex.WaitOne();

                currentLogLines++;
                if (currentLogLines > maxLogLines)
                {
                    // Display the line to the console.
                    // Skip writing the line to the log file.
                    Console.WriteLine("StdOut: {0}", outLine.Data);
                }
                else if (currentLogLines ==
 maxLogLines)
                {
                    LogToFile("StdOut", "<Max build log limit reached!>",
 
                        true);
    
                    // Stop reading the output streams.
                    Process p = sendingProcess as Process;
                    if (p != null)
                    {
                        p.CancelOutputRead();
                        p.CancelErrorRead();
                    }
                }
                else 
                {
                    // Write the line to the log file.
                    LogToFile("StdOut", outLine.Data, true);
                }
                logMutex.ReleaseMutex();
            }
        }

        private static void
 NMakeErrorDataHandler(object sendingProcess, 
            DataReceivedEventArgs errLine)
        {
            // Collect error output, displaying it to the screen and
 
            // logging it to the output file.  Cancel the error output
            // read operation when the maximum line limit is reached.

            if (!String.IsNullOrEmpty(errLine.Data))
            {
                logMutex.WaitOne();

                currentLogLines++;
                if (currentLogLines > maxLogLines)
                {
                    // Display the error line to the console.
                    // Skip writing the line to the log file.
                    Console.WriteLine("StdErr: {0}", errLine.Data);
                }
                else if (currentLogLines ==
 maxLogLines)
                {
                    LogToFile("StdErr", "<Max build log limit reached!>",
 
                        true);
    
                    // Stop reading the output streams
                    Process p = sendingProcess as Process;
                    if (p != null)
                    {
                        p.CancelErrorRead();
                        p.CancelOutputRead();

                    }
                }
                else 
                {
                    // Write the line to the log file.
                    LogToFile("StdErr", errLine.Data, true);
                }

                logMutex.ReleaseMutex();
            }
        }

        private static void
 LogToFile(String logPrefix, 
            String logText, bool echoToConsole)
        {
            // Write the specified line to the log file stream.
            StringBuilder logString = new StringBuilder();

            if (!String.IsNullOrEmpty(logPrefix))
            {
                logString.AppendFormat("{0}> ", logPrefix);
            }

            if (!String.IsNullOrEmpty(logText))
            {
                logString.Append(logText); 
            }

            if (buildLogStream != null)
            {
                buildLogStream.WriteLine("[{0}] {1}",
                    DateTime.Now.ToString(), logString.ToString());
                buildLogStream.Flush();
            }
            
            if (echoToConsole)
            {
                Console.WriteLine(logString.ToString());
            }
        }
    }
} 
ref class ProcessNMakeStreamRedirection
{
private:
   // Define static variables shared by class methods.
   static StreamWriter^ buildLogStream = nullptr;
   static Mutex^ logMutex = gcnew Mutex;
   static int maxLogLines = 25;
   static int currentLogLines = 0;

public:
   static void RedirectNMakeCommandStreams()
   {
      String^ nmakeArguments = nullptr;
      Process^ nmakeProcess;
      
      // Get the input nmake command-line arguments.
      Console::WriteLine( "Enter the NMake command line arguments (@commandfile
 or /f makefile, etc):" );
      String^ inputText = Console::ReadLine();
      if (  !String::IsNullOrEmpty( inputText ) )
      {
         nmakeArguments = inputText;
      }

      Console::WriteLine( "Enter max line limit for log file
 (default is 25):" );
      inputText = Console::ReadLine();
      if (  !String::IsNullOrEmpty( inputText ) )
      {
         if (  !Int32::TryParse( inputText, maxLogLines ) )
         {
            maxLogLines = 25;
         }
      }
      Console::WriteLine( "Output beyond {0} lines will be ignored.",
         maxLogLines.ToString() );
      
      // Initialize the process and its StartInfo properties.
      nmakeProcess = gcnew Process;
      nmakeProcess->StartInfo->FileName = "NMake.exe";
      
      // Build the nmake command argument list.
      if (  !String::IsNullOrEmpty( nmakeArguments ) )
      {
         nmakeProcess->StartInfo->Arguments = nmakeArguments;
      }
      
      // Set UseShellExecute to false for redirection.
      nmakeProcess->StartInfo->UseShellExecute = false;
      
      // Redirect the standard output of the nmake command.  
      // Read the stream asynchronously using an event handler.
      nmakeProcess->StartInfo->RedirectStandardOutput = true;
      nmakeProcess->OutputDataReceived += gcnew DataReceivedEventHandler( NMakeOutputDataHandler
 );
      
      // Redirect the error output of the nmake command. 
      nmakeProcess->StartInfo->RedirectStandardError = true;
      nmakeProcess->ErrorDataReceived += gcnew DataReceivedEventHandler( NMakeErrorDataHandler
 );

      logMutex->WaitOne();

      currentLogLines = 0;
      
      // Write a header to the log file.
      String^ buildLogFile = "NmakeCmd.Txt";
      try
      {
         buildLogStream = gcnew StreamWriter( buildLogFile,true
 );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Could not open output file {0}", buildLogFile
 );
         Console::WriteLine( "Exception = {0}", e->ToString() );
         Console::WriteLine( e->Message->ToString() );

         buildLogStream = nullptr;
      }

      if ( buildLogStream != nullptr )
      {
         Console::WriteLine( "Nmake output logged to {0}", buildLogFile
 );

         buildLogStream->WriteLine();
         buildLogStream->WriteLine( DateTime::Now.ToString() );
         if (  !String::IsNullOrEmpty( nmakeArguments ) )
         {
            buildLogStream->Write( "Command line = NMake {0}", nmakeArguments
 );
         }
         else
         {
            buildLogStream->Write( "Command line = Nmake" );
         }
         buildLogStream->WriteLine();
         buildLogStream->Flush();

         logMutex->ReleaseMutex();
         
         // Start the process.
         Console::WriteLine();
         Console::WriteLine( "\nStarting Nmake command" );
         Console::WriteLine();
         nmakeProcess->Start();
         
         // Start the asynchronous read of the output stream.
         nmakeProcess->BeginOutputReadLine();
         
         // Start the asynchronous read of the error stream.
         nmakeProcess->BeginErrorReadLine();
         
         // Let the nmake command run, collecting the output.
         nmakeProcess->WaitForExit();

         nmakeProcess->Close();
         buildLogStream->Close();
      }
   }

private:
   static void NMakeOutputDataHandler( Object^
 sendingProcess,
      DataReceivedEventArgs^ outLine )
   {
      // Collect the output, displaying it to the screen and 
      // logging it to the output file.  Cancel the read
      // operation when the maximum line limit is reached.

      if (  !String::IsNullOrEmpty( outLine->Data ) )
      {
         logMutex->WaitOne();

         currentLogLines++;
         if ( currentLogLines > maxLogLines )
         {
            // Display the line to the console.
            // Skip writing the line to the log file.
            Console::WriteLine( "StdOut: {0}", outLine->Data->ToString()
 );
         }
         else
         if ( currentLogLines == maxLogLines )
         {
            LogToFile( "StdOut", "<Max build log limit reached!>",
 true );
            
            // Stop reading the output streams.
            Process^ p = dynamic_cast<Process^>(sendingProcess);
            if ( p != nullptr )
            {
               p->CancelOutputRead();
               p->CancelErrorRead();
            }
         }
         else
         {
            // Write the line to the log file.
            LogToFile( "StdOut", outLine->Data, true
 );
         }
         logMutex->ReleaseMutex();
      }
   }

   static void NMakeErrorDataHandler( Object^
 sendingProcess,
      DataReceivedEventArgs^ errLine )
   {
      
      // Collect the error output, displaying it to the screen and 
      // logging it to the output file.  Cancel the error output
      // read operation when the maximum line limit is reached.

      if (  !String::IsNullOrEmpty( errLine->Data ) )
      {
         logMutex->WaitOne();

         currentLogLines++;
         if ( currentLogLines > maxLogLines )
         {
            
            // Display the line to the console.
            // Skip writing the line to the log file.
            Console::WriteLine( "StdErr: {0}", errLine->Data->ToString()
 );
         }
         else
         if ( currentLogLines == maxLogLines )
         {
            LogToFile( "StdOut", "<Max build log limit reached!>",
 true );
            
            // Stop reading the output streams.
            Process^ p = dynamic_cast<Process^>(sendingProcess);
            if ( p != nullptr )
            {
               p->CancelOutputRead();
               p->CancelErrorRead();
            }
         }
         else
         {
            // Write the line to the log file.
            LogToFile( "StdErr", errLine->Data, true
 );
         }
         logMutex->ReleaseMutex();
      }
   }

   static void LogToFile( String^ logPrefix,
 String^ logText,
      bool echoToConsole )
   {
      // Write the specified line to the log file stream.
      StringBuilder^ logString = gcnew StringBuilder;

      if (  !String::IsNullOrEmpty( logPrefix ) )
      {
         logString->AppendFormat( "{0}> ", logPrefix );
      }

      if (  !String::IsNullOrEmpty( logText ) )
      {
         logString->Append( logText );
      }

      if ( buildLogStream != nullptr )
      {
         buildLogStream->WriteLine(  "[{0}] {1}",
            DateTime::Now.ToString(), logString->ToString() );
         buildLogStream->Flush();
      }

      if ( echoToConsole )
      {
         Console::WriteLine( logString->ToString() );
      }
   }
};
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Process クラス
Process メンバ
System.Diagnostics 名前空間
BeginErrorReadLine
ProcessStartInfo.RedirectStandardError
StandardError
ErrorDataReceived
DataReceivedEventHandler デリゲート


このページでは「.NET Framework クラス ライブラリ リファレンス」からProcess.CancelErrorRead メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からProcess.CancelErrorRead メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からProcess.CancelErrorRead メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Process.CancelErrorRead メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Process.CancelErrorRead メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS