Named pipe stalls threads?

Posted by entens on Stack Overflow See other posts from Stack Overflow or by entens
Published on 2010-05-13T15:18:35Z Indexed on 2010/05/13 15:24 UTC
Read the original article Hit count: 215

Filed under:
|

I am attempting to push updates into a process via a named pipe, but in doing so my process loop now seams to stall on while ((line = sr.ReadLine()) != null). I'm a little mystified as to what might be wrong as this is my first foray into named pipes.

void RefreshThread()
{
    using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("processPipe", PipeDirection.In))
    {
        pipeStream.WaitForConnection();

        using (StreamReader sr = new StreamReader(pipeStream))
        {
            for (; ; )
            {
                if (StopThread == true)
                {
                    StopThread = false;
                    return;                 // exit loop and terminate the thread
                }

                // push update for heartbeat
                int HeartbeatHandle = ItemDictionary["Info.Heartbeat"];
                int HeartbeatValue = (int)Config.Items[HeartbeatHandle].Value;
                Config.Items[HeartbeatHandle].Value = ++HeartbeatValue;
                SetItemValue(HeartbeatHandle, HeartbeatValue, (short)0xC0, DateTime.Now);

                string line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    // line is in the format: item, value, timestamp
                    string[] parts = line.Split(',');

                    // push update and store value in item cache
                    int handle = ItemDictionary[parts[0]];
                    object value = parts[1];
                    Config.Items[handle].Value = int.Parse(value);
                    DateTime timestamp = DateTime.FromBinary(long.Parse(parts[2]));
                    SetItemValue(handle, value, (short)0xC0, timestamp);
                }

                Thread.Sleep(500);
            }
        }
    }
}

© Stack Overflow or respective owner

Related posts about named-pipes

Related posts about c#