Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 685431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:55:19+00:00 2026-05-14T01:55:19+00:00

I need to find a way to be notified when a System.IO.Pipe.NamedPipeServerStream opened in

  • 0

I need to find a way to be notified when a System.IO.Pipe.NamedPipeServerStream opened in asynchronous mode has more data available for reading on it- a WaitHandle would be ideal. I cannot simply use BeginRead() to obtain such a handle because it’s possible that i might be signaled by another thread which wants to write to the pipe- so I have to release the lock on the pipe and wait for the write to be complete, and NamedPipeServerStream doesnt have a CancelAsync method. I also tried calling BeginRead(), then calling the win32 function CancelIO on the pipe if the thread gets signaled, but I don’t think this is an ideal solution because if CancelIO is called just as data is arriving and being processed, it will be dropped- I still wish to keep this data, but process it at a later time, after the write. I suspect the win32 function PeekNamedPipe might be useful but i’d like to avoid having to continuously poll for new data with it.

In the likley event that the above text is a bit unclear, here’s roughly what i’d like to be able to do…

NamedPipeServerStream pipe;
ManualResetEvent WriteFlag;
//initialise pipe
lock (pipe)
{
    //I wish this method existed
    WaitHandle NewDataHandle = pipe.GetDataAvailableWaithandle();
    Waithandle[] BreakConditions = new Waithandle[2];
    BreakConditions[0] = NewDataHandle;
    BreakConditions[1] = WriteFlag;
    int breakcode = WaitHandle.WaitAny(BreakConditions);
    switch (breakcode)
    {
        case 0:
            //do a read on the pipe
            break;
        case 1:
            //break so that we release the lock on the pipe
            break;
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:55:19+00:00Added an answer on May 14, 2026 at 1:55 am

    Ok, so I just ripped this out of my code, hopefully I deleted all the application logic stuff. The idea is that you try a zero-length read with ReadFile and wait on both the lpOverlapped.EventHandle (fired when the read completes) and a WaitHandle set when another thread wants to write to the pipe. If the read is to be interrupted due to a writing thread, use CancelIoEx to cancel the zero-length read.

    NativeOverlapped lpOverlapped;
    ManualResetEvent DataReadyHandle = new ManualResetEvent(false);
    lpOverlapped.InternalHigh = IntPtr.Zero;
    lpOverlapped.InternalLow = IntPtr.Zero;
    lpOverlapped.OffsetHigh = 0;
    lpOverlapped.OffsetLow = 0;
    lpOverlapped.EventHandle = DataReadyHandle.SafeWaitHandle.DangerousGetHandle();
    IntPtr x = Marshal.AllocHGlobal(1); //for some reason, ReadFile doesnt like passing NULL in as a buffer
    bool rval = ReadFile(SerialPipe.SafePipeHandle, x, 0, IntPtr.Zero,
       ref lpOverlapped);
    int BreakCause;
    if (!rval) //operation is completing asynchronously
    {
       if (GetLastError() != 997) //ERROR_IO_PENDING, which is in fact good
          throw new IOException();
       //So, we have a list of conditions we are waiting for
       WaitHandle[] BreakConditions = new WaitHandle[3];
       //We might get some input to read from the serial port...
       BreakConditions[0] = DataReadyHandle;
        //we might get told to yield the lock so that CPU can write...
       BreakConditions[1] = WriteRequiredSignal;
       //or we might get told that this thread has become expendable
       BreakConditions[2] = ThreadKillSignal;
       BreakCause = WaitHandle.WaitAny(BreakConditions, timeout);
    }
    else //operation completed synchronously; there is data available
    {
       BreakCause = 0; //jump into the reading code in the switch below
    }
    switch (BreakCause)
    {
       case 0:
          //serial port input
          byte[] Buffer = new byte[AttemptReadSize];
          int BRead = SerialPipe.Read(Buffer, 0, AttemptReadSize);
          //do something with your bytes.
          break;
       case 1:
          //asked to yield
          //first kill that read operation
          CancelIoEx(SerialPipe.SafePipeHandle, ref lpOverlapped);
          //should hand over the pipe mutex and wait to be told to tkae it back
          System.Threading.Monitor.Exit(SerialPipeLock);
          WriteRequiredSignal.Reset();
          WriteCompleteSignal.WaitOne();
          WriteCompleteSignal.Reset();
          System.Threading.Monitor.Enter(SerialPipeLock);
          break;
       case 2:
          //asked to die
          //we are the ones responsible for cleaning up the pipe
          CancelIoEx(SerialPipe.SafePipeHandle, ref lpOverlapped);
          //finally block will clean up the pipe and the mutex
          return; //quit the thread
    }
    Marshal.FreeHGlobal(x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find a way to determine what object in a database has
I need to find a way, such that user has to input 2 numbers
I am using primefaces datatable with clickable rows and I need to find way
I need to find a way to get actual page size in Google Chrome.
I need to find a way to check if the mouse moves in c#,
I need to find a way to spin off a thread from a static
I need to find a way to call a vb.net function in my aspx
I need to find some way of displaying a drop-down menu that depending on
I need to find a way to store 250 KB of plain text numbers
I need to find a way to use with(nolock) in every SELECT I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.