954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to unblock ConnectNamedPipe and ReadFile? [C#]

I have a class (NamedPipeManager) which has a thread (PipeThread) that waits for a NamedPipe connection using (ConnectNamedPipe) and then reads (ReadFile) - these are blocking calls (not-overlapped) - however there comes a point when I want to unblock them - for example when the calling class tries to stop the NamedPipeManager...

How can I interupt it? Using Thread.abort? Thread.interrupt? Is there a proper way to handle this?
Refer to the code below which illustrates my current situation

main()
    {
        NamedPipeManager np = new NamedPipeManager();
            ... do stuff ...
        ... do stuff ...
        np.Stop();		// at this point I want to stop waiting on a connection
    }


    class NamedPipeManager
    {
    private Thread PipeThread;
    
    public NamedPipeManager
    {
        PipeThread = new Thread(new ThreadStart(ManagePipes));
        PipeThread.IsBackground = true;
        PipeThread.Name = "NamedPipe Manager";
        PipeThread.Start();
    }

    private void ManagePipes()
    {
        handle = CreateNamedPipe(..., PIPE_WAIT, ...);
        ConnectNamedPipe(handle, null);		// this is the BLOCKING call waiting for client connection
        
        ReadFile(....);				// this is the BLOCKING call to readfile after a connection has been established
        }


    public void Stop()
    {
        /// This is where I need to do my magic
        /// But somehow I need to stop PipeThread
        PipeThread.abort();		//?? my gut tells me this is bad
    }
    };


So, in function Stop() - how would I gracefully unblock the call to ConnectNamedPipe(...) or ReadFile(...)?

Any help would be appreciated.
Thanks,

Shaitan00
Light Poster
38 posts since Jul 2006
Reputation Points: 9
Solved Threads: 0
 

Once before and now again
What makes your posts so damn important that they must always be asked on two forums in parallel?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Again - asI asked you previously - is there a rule againt doing it?
Most often people on the 2 different forums are not the same, and I get various answers.

So please - if this is a rule let me know - otherwise please explain why this causes you such an issue ...

Shaitan00
Light Poster
38 posts since Jul 2006
Reputation Points: 9
Solved Threads: 0
 

http://www.catb.org/~esr/faqs/smart-questions.html#forum
Told you this before, apparently it doesn't sink in.

Over on devshed, where you also target your cross-posting, this is in the "New users - HOW TO POST A QUESTION - READ THIS FIRST" thread.
Apparently you didn't.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You