2012-02-04 3 views
8

उपयोग किए जाने पर बस लटकता है मैं पहली बार नामित पाइप का उपयोग करने की कोशिश कर रहा हूं। एमएस प्रलेखन here पाया में, यह कहा गया है कि:NamedPipeServerStream.EndWaitForConnection()

EndWaitForConnection BeginWaitForConnection के लिए हर कॉल के लिए ठीक एक बार बुलाया जाना चाहिए।

तो मैं एक अच्छा थोड़ा प्रोग्रामर हो सकता है और दस्तावेज़ का पालन करने की कोशिश कर रहा हूँ, लेकिन जब मैं इसका इस्तेमाल EndWaitForConnection() सिर्फ अनिश्चित काल के लिए लटकी हुई है।

इसलिए मैंने अपना कोड कम से कम घटा दिया ताकि देखें कि क्या मैं समस्या को अलग कर सकता हूं लेकिन कोई पासा नहीं। मैंने लिखे गए वर्ग से निम्नलिखित कोड खींचा है। मैं इसे संशोधित किया है जिससे वह तो एक पाइप कनेक्शन पर इंतजार कर शुरू होता है तुरंत कि पाइप कनेक्शन पर इंतजार कर बंद करने के लिए कोशिश करता है:

private void WaitForConnectionCallBack(IAsyncResult result) 
{ 

} 

public void Start() 
{ 
    var tempPipe = new NamedPipeServerStream("TempPipe", 
              PipeDirection.In, 
              254, 
              PipeTransmissionMode.Message, 
              PipeOptions.Asynchronous); 

    IAsyncResult result = tempPipe.BeginWaitForConnection(
            new AsyncCallback(WaitForConnectionCallBack), this); 

    tempPipe.EndWaitForConnection(result); // <----- Hangs on this line right here 
} 

1) क्यों यह EndWaitForConnection() पर लटका करता है? यदि मैं कनेक्शन प्राप्त करने से पहले अपने सर्वर को बंद करना चाहता हूं, तो मैं अनिवार्य रूप से इस BeginWaitForConnection() कॉलबैक को कैसे रद्द कर सकता हूं?

2) मान लीजिए कि मेरे ऊपर उल्लिखित समस्या नहीं है। क्या होता है यदि 2 क्लाइंट मेरे नामित पाइप से कनेक्ट करने का प्रयास करते हैं?

क्या मुझे उनमें से प्रत्येक के लिए कॉलबैक आमंत्रण मिलता है, या मुझे पहले कनेक्शन अधिसूचना प्राप्त करने के लिए इंतजार करना है, तो फिर अगले ग्राहक को फिर से सुनना शुरू करने के लिए EndWaitForConnection() फिर WaitForConnectionCallBack() पर कॉल करें?

उत्तरार्द्ध मुझे दौड़ की स्थिति की तरह लगता है, क्योंकि मैं कनेक्शन श्रोता को पर्याप्त तेज़ी से स्थापित नहीं कर सकता।

+0

डिज़ाइन द्वारा, कॉल केवल आपके कॉलबैक विधि (WaitForConnectionCallBack) में उपयोग की जानी चाहिए। आप tempPipe.Close() को कॉल करके इसे रद्द कर दें। –

+0

हां, मैं उस निष्कर्ष पर खुद का हुआ। मुझे मूल रूप से पता चला कि tempPipe.Close() को कॉलबैक रूटीन तुरंत चल रहा है, समस्या यह है कि मैंने इसे एंडवेटफॉरकनेक्शन को तुरंत कॉल करने के लिए स्थापित किया है, लेकिन तब से पाइप बंद हो जाने के बाद, यह अपवाद फेंकता है। तो मुझे इसके चारों ओर एक कोशिश कथन लपेटना पड़ा और पकड़ बयान में कुछ भी नहीं करना पड़ा। क्या यह सही समाधान है? यह पाइप को बंद करने के लिए मेरे लिए थोड़ा सा मैला लगता है, यह जानकर कि यह आपके कॉलबैक में अपवाद को मजबूर करेगा जो आपको पकड़ना होगा। – Ultratrunks

+0

यह पूरी तरह से सामान्य है। –

उत्तर

8

तो, समाधान है कि मेरे लिए काम कर रहा है की एक बुनियादी कंकाल इस प्रकार है:

private void WaitForConnectionCallBack(IAsyncResult result) 
{ 
    try 
    { 
     PipeServer.EndWaitForConnection(result); 

     /// ... 
     /// Some arbitrary code 
     /// ... 
    } 
    catch 
    { 
     // If the pipe is closed before a client ever connects, 
     // EndWaitForConnection() will throw an exception. 

     // If we are in here that is probably the case so just return. 
     return; 
    } 
} 

यहाँ सर्वर कोड है।

public void Start() 
{ 
    var server= new NamedPipeServerStream("TempPipe", 
              PipeDirection.In, 
              254, 
              PipeTransmissionMode.Message, 
              PipeOptions.Asynchronous); 

    // If nothing ever connects, the callback will never be called. 
    server.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), this); 

    // ... arbitrary code 

// EndWaitForConnection() was not the right answer here, it would just wait indefinitely 
// if you called it. As Hans Passant mention, its meant to be used in the callback. 
// Which it now is. Instead, we are going to close the pipe. This will trigger 
// the callback to get called. 

// However, the EndWaitForConnection() that will excecute in the callback will fail 
// with an exception since the pipe is closed by time it gets invoked, 
// thus you must capture it with a try/catch 

    server.Close(); // <--- effectively closes our pipe and gets our 
         //  BeginWaitForConnection() moving, even though any future 
         //  operations on the pipe will fail. 
} 
+0

FYI देखें [सी # अनधिकृत एक्सेस अपवाद जब केवल पढ़ने के लिए नामित पाइप (नामांकित पाइप क्लाइंटस्ट्रीम क्लास) के लिए संदेश मोड सक्षम करता है] (http://stackoverflow.com/questions/32739224/c-sharp-unauthorizedaccessexception-when-enabling-messagemode-for-read-only -नाम) 'संदेश' मोड का उपयोग करने के बारे में अन्य जानकारी के लिए। – OmegaMan

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

  • कोई संबंधित समस्या नहीं^_^