Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel NamedPipeClientStream.Read call

I've looked around all over the web, but I can't find an answer to the following question.

I have a C#/.NET NamedPipeClientStream instance in a client program, and a worker thread is calling NamedPipeClientStream.Read(byte[], int, int) to get data from a server. The server sends data updates to the client.

Read is a blocking call. If I want to close the client, is there a way to cancel/exit the Read call? I have tried calling Close on the named pipe instance, but it has no effect on the thread that called Read.

I would think there would be a way to cancel a Read call. If not, it seems like that is a very poorly designed API, because your program is at the mercy of the pipe.

Any info is greatly appreciated.

-Chris

like image 805
Chris Avatar asked Mar 01 '10 19:03

Chris


1 Answers

Use the NamedPipeClientStream constructor that takes a PipeOptions argument. Specifying PipeOptions.Asynchronous will complete the Read() call when you call the Close() method. The Read() method returns 0.

like image 182
Hans Passant Avatar answered Sep 19 '22 13:09

Hans Passant