我现在正在使用命名管道技术来实现我的学术项目软件,以通过网络连接异构系统。我使用.net框架4和C#语言。问题是,如果服务器未准备好或不可用,客户端程序将无法继续。客户端命名管道不断尝试连接到服务器命名管道,直到可用连接为止。
我希望如果服务器连接在 3 秒内(可以是任何持续时间)不可用,客户端程序能够继续执行其他功能。就像这样的场景:当客户端程序启动时,它将尝试连接到服务器。如果服务器不可用,客户端将停止尝试连接到服务器并自行离线运行。
我的问题的一些代码片段...
pipeClient.Connect(); <-- this is the problem point,
frmUserprofile.show(); <-- until the connection is available, the program will not execute this line
Run Code Online (Sandbox Code Playgroud)
我想要得到的解决方案...
pipeClient.Connect()
if (3 seconds is over && server connection is unavailable) <-- this is what I need
{ pipeClient stops try to connect; }
frmUserprofile.show();
Run Code Online (Sandbox Code Playgroud)
有人可以帮我给我一些实际的解决方案吗...顺便说一句,我希望如果你能用 C# 语言解决这个问题,请用 C# 给出答案,但不一定提前感谢...
如果您使用NamedPipeClientStream类,则存在Connect(int)方法重载,它接受超时值:
bool isConnected = false;
try
{
pipeClient.Connect(3000);
isConnected = true;
}
catch(TimeoutException)
{
// failed to connect
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3909 次 |
| 最近记录: |