[英]how to stop trying to connect by namedpipe client to server if the server is not ready or unavailable
我现在正在使用命名管道技术实现我的学术项目软件,以通过网络连接异构系统。 我使用.net框架4和C#语言。 问题是如果服务器没有准备好或不可用,客户端程序将无法继续。 名为pipe的客户端不断尝试连接到名为pipe的服务器,直到可用连接。
如果服务器连接在3秒内没有可用(可能是任何持续时间),我希望客户端程序能够继续其他功能。 像这样的场景:当客户端程序启动时,它将尝试连接到服务器。 如果服务器不可用,客户端将停止尝试连接到服务器并自行脱机运行。
我问题的一些代码片段......
pipeClient.Connect(); <-- this is the problem point,
frmUserprofile.show(); <-- until the connection is available, the program will not execute this line
我希望得到的解决方案......
pipeClient.Connect()
if (3 seconds is over && server connection is unavailable) <-- this is what I need
{ pipeClient stops try to connect; }
frmUserprofile.show();
有人可以帮助我给我一些实际的解决方案......通过各种方式,我希望如果你能用C#语言解决这个问题,请用C#给出答案,但不一定要提前感谢...
如果您使用的是NamedPipeClientStream
类,则会有Connect(int)
方法重载,它接受超时值:
bool isConnected = false;
try
{
pipeClient.Connect(3000);
isConnected = true;
}
catch(TimeoutException)
{
// failed to connect
}
如果对任务使用NamedPipeClientStream,则需要Connect(Int32)方法,该方法需要超时量
使用NamedPipeClientStream
。 它有一个连接超时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.