views:

160

answers:

2

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

A: 

Not a direct answer to your question, but - I had to do some NamedPipe IPC quite recently and I found that WCF was awesome. Took me less than an hour to implement a POC and yes, I'm pretty sure that it exposes means to cancel your request either - and you don't have to think in terms of byte arrays.

Is that's an option for you?

Benjamin Podszun
I looked into WCF a little bit and got really excited by it, but unfortunately the client I'm developing has to talk to a custom server (C++, not .NET) with its own predefined named pipes and communication protocol.When I was looking at WCF, I was quickly overwhelmed by the vastness of the documentation. I wasn't able to quickly determine if I could use WCF to talk to an existing system using named pipes with a custom protocol. If anybody knows if this is possible and can point me in the right direction, I would greatly appreciate it.
Chris
+2  A: 

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.

Hans Passant
:D You are my hero! Setting the pipe to be Asynchronous does cause a blocked call to Read to return when Close is called. Thanks a lot!
Chris