2009-01-22 11 views
10

नेट Console वर्ग और उसके डिफ़ॉल्ट TextWriter कार्यान्वयन (Console.Out के रूप में उपलब्ध है और परोक्ष जैसे Console.WriteLine() में) जब आवेदन इसके उत्पादन चल रहा है किसी भी त्रुटि का संकेत नहीं है किसी अन्य प्रोग्राम को पहुंचाया में पुनः निर्देशित सांत्वना उत्पादन में बंद पाइप का पता लगाने , और दूसरा प्रोग्राम आवेदन समाप्त होने से पहले पाइप को समाप्त या बंद कर देता है। इसका मतलब है कि एप्लिकेशन आवश्यक से अधिक समय तक चल सकता है, एक ब्लैक होल में आउटपुट लिख रहा है।NET अनुप्रयोगों

मैं पुनर्निर्देशन पाइप के दूसरे छोर को बंद करने का पता कैसे लगा सकता हूं?

एक अधिक विस्तृत विवरण इस प्रकार है:

यहां उदाहरण के कार्यक्रमों है कि समस्या का प्रदर्शन की एक जोड़ी है। पूर्णांकों का Produce प्रिंट बहुत सारे काफी धीरे-धीरे, गणना के प्रभाव अनुकरण करने के लिए:

using System; 
class Produce 
{ 
    static void Main() 
    { 
     for (int i = 0; i < 10000; ++i) 
     { 
      System.Threading.Thread.Sleep(100); // added for effect 
      Console.WriteLine(i); 
     } 
    } 
} 

Consume केवल इनपुट के पहले 10 लाइनों पढ़ता है और फिर बाहर निकल जाता है:

using System; 
class Consume 
{ 
    static void Main() 
    { 
     for (int i = 0; i < 10; ++i) 
      Console.ReadLine(); 
    } 
} 

इन दो कार्यक्रमों संकलित कर रहे हैं, और दूसरे के लिए पहली पाइप के उत्पादन में है, इसलिए जैसे:

Produce | Consume 

... यह है किमनाया जा सकता हैके बाद लंबे समय तक चल रहा है।

हकीकत में, मेरा Consume प्रोग्राम यूनिक्स-शैली head है, और मेरा Produce प्रोग्राम प्रिंट करता है जो गणना करने के लिए महंगा है। मैं आउटपुट को समाप्त करना चाहता हूं जब पाइप के दूसरे छोर ने कनेक्शन बंद कर दिया है।

मैं .NET में यह कैसे कर सकता हूं?

(मुझे पता है कि एक स्पष्ट विकल्प आउटपुट को सीमित करने के लिए कमांड लाइन तर्क पास करना है, और वास्तव में मैं यही कर रहा हूं, लेकिन मैं अभी भी यह जानना चाहता हूं कि मैं ऐसा कैसे करना चाहता हूं जब पढ़ने को समाप्त करने के बारे में अधिक विन्यास निर्णय करने में सक्षम; head से पहले grep के माध्यम से जैसे पाइपिंग)

अद्यतन:। इसमें .NET में System.IO.__ConsoleStream कार्यान्वयन की तरह बुरी तरह लग रहा है हार्ड-कोडेड त्रुटियों को अनदेखा करने के लिए 0x6D (ERROR_BROKEN_PIPE) और 0xE8 (ERROR_NO_DATA)। इसका शायद मतलब है कि मुझे कंसोल स्ट्रीम को फिर से लागू करने की आवश्यकता है। Sigh ...)

+0

यदि आप इसे आगे बढ़ाने का निर्णय लेते हैं तो मुझे आपके समाधान में बहुत दिलचस्पी होगी। –

उत्तर

7

यह एक हल करने के लिए, मैं लिखना पड़ा मेरी Win32 फ़ाइल हैंडल पर अपने मूल स्ट्रीम कार्यान्वयन। यह बहुत मुश्किल नहीं था, क्योंकि मुझे एसिंक्रोनस सपोर्ट, बफरिंग या मांग को लागू करने की आवश्यकता नहीं थी।

दुर्भाग्यवश, असुरक्षित कोड का उपयोग करने की आवश्यकता है, लेकिन आमतौर पर यह कंसोल अनुप्रयोगों के लिए कोई समस्या नहीं है जो स्थानीय रूप से और पूर्ण विश्वास के साथ चलाया जाएगा।

यहाँ कोर धारा है:

class HandleStream : Stream 
{ 
    SafeHandle _handle; 
    FileAccess _access; 
    bool _eof; 

    public HandleStream(SafeHandle handle, FileAccess access) 
    { 
     _handle = handle; 
     _access = access; 
    } 

    public override bool CanRead 
    { 
     get { return (_access & FileAccess.Read) != 0; } 
    } 

    public override bool CanSeek 
    { 
     get { return false; } 
    } 

    public override bool CanWrite 
    { 
     get { return (_access & FileAccess.Write) != 0; } 
    } 

    public override void Flush() 
    { 
     // use external buffering if you need it. 
    } 

    public override long Length 
    { 
     get { throw new NotSupportedException(); } 
    } 

    public override long Position 
    { 
     get { throw new NotSupportedException(); } 
     set { throw new NotSupportedException(); } 
    } 

    static void CheckRange(byte[] buffer, int offset, int count) 
    { 
     if (offset < 0 || count < 0 || (offset + count) < 0 
      || (offset + count) > buffer.Length) 
      throw new ArgumentOutOfRangeException(); 
    } 

    public bool EndOfStream 
    { 
     get { return _eof; } 
    } 

    public override int Read(byte[] buffer, int offset, int count) 
    { 
     CheckRange(buffer, offset, count); 
     int result = ReadFileNative(_handle, buffer, offset, count); 
     _eof |= result == 0; 
     return result; 
    } 

    public override void Write(byte[] buffer, int offset, int count) 
    { 
     int notUsed; 
     Write(buffer, offset, count, out notUsed); 
    } 

    public void Write(byte[] buffer, int offset, int count, out int written) 
    { 
     CheckRange(buffer, offset, count); 
     int result = WriteFileNative(_handle, buffer, offset, count); 
     _eof |= result == 0; 
     written = result; 
    } 

    public override long Seek(long offset, SeekOrigin origin) 
    { 
     throw new NotSupportedException(); 
    } 

    public override void SetLength(long value) 
    { 
     throw new NotSupportedException(); 
    } 

    [return: MarshalAs(UnmanagedType.Bool)] 
    [DllImport("kernel32", SetLastError=true)] 
    static extern unsafe bool ReadFile(
     SafeHandle hFile, byte* lpBuffer, int nNumberOfBytesToRead, 
     out int lpNumberOfBytesRead, IntPtr lpOverlapped); 

    [return: MarshalAs(UnmanagedType.Bool)] 
    [DllImport("kernel32.dll", SetLastError=true)] 
    static extern unsafe bool WriteFile(
     SafeHandle hFile, byte* lpBuffer, int nNumberOfBytesToWrite, 
     out int lpNumberOfBytesWritten, IntPtr lpOverlapped); 

    unsafe static int WriteFileNative(SafeHandle hFile, byte[] buffer, int offset, int count) 
    { 
     if (buffer.Length == 0) 
      return 0; 

     fixed (byte* bufAddr = &buffer[0]) 
     { 
      int result; 
      if (!WriteFile(hFile, bufAddr + offset, count, out result, IntPtr.Zero)) 
      { 
       // Using Win32Exception just to get message resource from OS. 
       Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error()); 
       int hr = ex.NativeErrorCode | unchecked((int) 0x80000000); 
       throw new IOException(ex.Message, hr); 
      } 

      return result; 
     } 
    } 

    unsafe static int ReadFileNative(SafeHandle hFile, byte[] buffer, int offset, int count) 
    { 
     if (buffer.Length == 0) 
      return 0; 

     fixed (byte* bufAddr = &buffer[0]) 
     { 
      int result; 
      if (!ReadFile(hFile, bufAddr + offset, count, out result, IntPtr.Zero)) 
      { 
       Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error()); 
       int hr = ex.NativeErrorCode | unchecked((int) 0x80000000); 
       throw new IOException(ex.Message, hr); 
      } 
      return result; 
     } 
    } 
} 

BufferedStream बफरिंग के लिए यह चारों ओर लिपटा जा सकता है अगर जरूरत है, लेकिन सांत्वना उत्पादन के लिए, TextWriter कर दिया जाएगा चरित्र स्तरीय बफरिंग वैसे भी, और केवल नई-पंक्तियों पर निस्तब्धता।

स्ट्रीम को कॉल करने के बजाय त्रुटि संदेश निकालने के लिए Win32Exception का दुरुपयोग करता है।

static class ConsoleStreams 
{ 
    enum StdHandle 
    { 
     Input = -10, 
     Output = -11, 
     Error = -12, 
    } 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern IntPtr GetStdHandle(int nStdHandle); 

    static SafeHandle GetStdHandle(StdHandle h) 
    { 
     return new SafeFileHandle(GetStdHandle((int) h), true); 
    } 

    public static HandleStream OpenStandardInput() 
    { 
     return new HandleStream(GetStdHandle(StdHandle.Input), FileAccess.Read); 
    } 

    public static HandleStream OpenStandardOutput() 
    { 
     return new HandleStream(GetStdHandle(StdHandle.Output), FileAccess.Write); 
    } 

    public static HandleStream OpenStandardError() 
    { 
     return new HandleStream(GetStdHandle(StdHandle.Error), FileAccess.Write); 
    } 

    static TextReader _in; 
    static StreamWriter _out; 
    static StreamWriter _error; 

    public static TextWriter Out 
    { 
     get 
     { 
      if (_out == null) 
      { 
       _out = new StreamWriter(OpenStandardOutput()); 
       _out.AutoFlush = true; 
      } 
      return _out; 
     } 
    } 

    public static TextWriter Error 
    { 
     get 
     { 
      if (_error == null) 
      { 
       _error = new StreamWriter(OpenStandardError()); 
       _error.AutoFlush = true; 
      } 
      return _error; 
     } 
    } 

    public static TextReader In 
    { 
     get 
     { 
      if (_in == null) 
       _in = new StreamReader(OpenStandardInput()); 
      return _in; 
     } 
    } 
} 

अंतिम परिणाम है कि पाइप के दूसरे छोर के बाद सांत्वना उत्पादन के लिए लिख समाप्त कर दिया है:

बिल्डिंग इस धारा पर, मैं सांत्वना के लिए एक सरल आवरण लिखने में सक्षम मैं/हे था कनेक्शन, संदेश के साथ एक अच्छा अपवाद में परिणाम:

पाइप

बंद किया जा रहा है को पकड़ने और 01 की उपेक्षा करके बाहरी स्तर पर, ऐसा लगता है कि मैं जाने के लिए अच्छा हूं।

1

मैं सहमत हूं कि ERROR_BROKEN_PIPE और ERROR_NO_DATA त्रुटियों की रिपोर्टिंग के बिना, __ConsoleStream आपके लिए कोई उपयोग नहीं है। मैं उत्सुक हूं कि उन्होंने इसे छोड़ने का फैसला क्यों किया।

जो लोग साथ पालन करने के लिए, __ConsoleStream की एक नहीं बल्कि पुराने, लेकिन फिर भी प्रासंगिक लिस्टिंग के लिए नीचे दिए गए लिंक की जाँच चाहते हैं के लिए ...

http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=42958

संबंधित मुद्दे