CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question 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

    Code:
        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,

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: How to unblock ConnectNamedPipe and ReadFile? [C#]

    check if there are any parameters in the ConnectNamedPipe or ReadFile() function to UNBLOCK those calls...

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How to unblock ConnectNamedPipe and ReadFile? [C#]

    Closing the handle will cause the methods to exit with an error.

    How are you creating the pipe ? If you're using this windows API call it accepts a parameter as to whether you want the pipe to be blocking or non-blocking.

    There'a also an example of a multi-threaded pipe server in C++ here.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Nov 2008
    Posts
    15

    Re: How to unblock ConnectNamedPipe and ReadFile? [C#]

    Is there a general solution to 'release the blocking code in a thread' problem?
    I mean, in a worker thread I am using someObject.SomeMethod(), which blocks and is the only option I can use, and I want to stop that worker thread anytime from my main thread; even if the worker thread is blocked at the point where someObject.SomeMethod() is called.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured