Nay Lin Aung
Nay Lin Aung

Reputation: 745

how to stop trying to connect by namedpipe client to server if the server is not ready or unavailable

I'm now implementing my academic project software using named pipe technologies to connect heterogeneous systems over the network. I used .net framework 4 and C# language. The problem is that the client program will not able to continue if the server is not ready or unavailable. The client named pipe continuously try to connect to the server named pipe until available connectivity.

I want the client program to be able to continue other functions if the server connection is not available within 3 seconds (may be any duration). Like this scenario: When the client program is started, it will try to connect to server. If the server is not available, the client will stop trying to connect to server and run offline by itself.

some code snippet of my problem...

pipeClient.Connect(); <-- this is the problem point,
frmUserprofile.show(); <-- until the connection is available, the program will not execute this line

the solution that I would like to get...

pipeClient.Connect()
if (3 seconds is over && server connection is unavailable) <-- this is what I need    
{  pipeClient stops try to connect;  }

frmUserprofile.show(); 

can someone help me to give some practical solution to me... by the ways, I hope if u can solve this problem with C# language, please give answers with C# but not necessarily thanks in advance...

Upvotes: 6

Views: 4095

Answers (3)

nunespascal
nunespascal

Reputation: 17724

Use the NamedPipeClientStream. It has a connect time out.

See NamedPipeClientStream.Connect

Upvotes: 0

Eugene Petrov
Eugene Petrov

Reputation: 651

If you use NamedPipeClientStream for your task there is Connect(Int32) method that takes amount of timeout

http://msdn.microsoft.com/en-us/library/bb355337.aspx

Upvotes: 0

max
max

Reputation: 34417

If you are using NamedPipeClientStream class, there is Connect(int) method overload, which accepts timeout value:

bool isConnected = false;
try
{
    pipeClient.Connect(3000);
    isConnected = true;
}
catch(TimeoutException)
{
    // failed to connect
}

Upvotes: 7

Related Questions