Featured post
How To Asynchronously Read/Write A Named Pipe In C# -
i have windows form hosts custom user control. user control fires seperate process (a .exe) creates , initializes namedpipeserverstream. once process initializes namedpipeserverstream, user control connects namedpipeclientstream.
this works fine.
on windows form have button called "check updates". when button pressed, namedpipeclientstream sends message server, server responds messagebox saying "i've been told check updates". can tell client > server communication working fine.
here's problem. server supposed send message client telling it's checking updates (so user control can update status after verying command received server). whenever happens, locks up.
now going assume because attempting both read , write named pipe @ same time?
below code snippets both server , client. (both these snippets run in seperate threads in respective processes not block ui)
gupdater (the named pipe server):
private void waitforclientcommands() { // wait connection named pipe client (the gupcontrol) pipestream.waitforconnection(); pipeconnected = true; // create streamwriter/streamreader can write/read named pipe pipestreamwriter = new streamwriter(pipestream) { autoflush = true }; pipestreamreader = new streamreader(pipestream); // have connection, start reading in messages , processing them while (true) { // skip time if writing pipe if(iswritingtopipe) continue; var message = pipestreamreader.readline(); if (message == null) { // don't want hog cpu time, if no message reaceived time, wait half second thread.sleep(500); continue; } switch(message) { case "checkforupdates": //messagebox.show("told check updates"); sendmessagetoclient("checking updates, woot!"); break; case "downloadupdate": messagebox.show("told download update"); break; case "applyupdate": messagebox.show("told apply update"); break; } } }
gupcontrol (the named pipe client):
private void waitforservercommands() { if(!pipeconnected) return; // have connection, start reading in messages , processing them while (true) { // skip time if writing pipe if (iswritingtopipe) continue; // attempt read line pipe var message = pipestreamreader.readline(); if (message == null) { // don't want hog cpu time, if no message reaceived time, wait half second thread.sleep(500); continue; } messagebox.show("i got message server!!\r\n" + message); } }
the following snippet method responsible writing client/server each component. (the difference in name, i.e. sendmessagetoclient
, sendmessagetoserver
)
private void sendmessagetoserver(string message) { if(pipeconnected) { iswritingtopipe = true; pipestreamwriter.writeline(message); iswritingtopipe = false; } }
the iswritingtopipe
variable simple bool true when respective process attempting write named pipe. initial attempt @ solving problem.
any appreciated!
system.io.pipes.pipestream
has method waitforpipedrain()
correct way manage coordination between client , server when exchanging messages.
the transmissionmode pipe may affecting things: using byte mode or message mode? i'm not sure how wrapping stream in streamreader plays pipe message mode semantics, example. see this answer more on message mode.
- Get link
- X
- Other Apps
Comments
Post a Comment