所以,我正在开发一个使用 TcpChannels 包含各种客户端和服务器的项目
并且有一个特定的操作是冻结服务器 - 使其看起来很慢 - 在解冻之前它不会响应任何远程调用。我用锁做这个,monitor.wait 和 monitor.pulse
问题是,即使我已经明确定义了通道“超时”属性,它在任何情况下都不会在冻结的服务器上过期。
所以这是一个简单的例子:
服务器 :
public class Server
static void Main(string[] args) {
HelloService myRem = null;
TcpChannel channel = new TcpChannel(8086);
ChannelServices.RegisterChannel(channel, true);
myRem = new HelloService();
RemotingServices.Marshal(myRem, "HelloService");
System.Console.WriteLine("<enter> to exit...");
System.Console.ReadLine();
}
public class HelloServer : MarshalByRef {
private bool freezed = true;
public string Hello() {
while (freezed)
lock (this)
Monitor.Wait(this);
return "Hello World!";
}
}
客户端:
public class Client
{
static void Main()
{
IDictionary propBag = new Hashtable();
propBag["name"] = "tpc";
propBag["timeout"] = 3000;
TcpChannel channel = new TcpChannel(propBag, null, null);
ChannelServices.RegisterChannel(channel, false);
HelloService obj = (HelloService)Activator.GetObject(typeof(HelloService), "tcp://localhost:8086/HelloService");
while (true)
{
try
{
Console.WriteLine(obj.Hello());
}
catch (Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine("TIMEOUT");
}
}
}
}
在我的电脑上 - 客户端的通话永远不会过期
直到我绝望地试探着删除了一个lock (this)
声明
它开始起作用了!收到的调用SocketException
但是现在我可以理解为什么因为Monitor.wait
必须始终在锁定语句中
为什么这在我的电脑上工作?我已经尝试过其他人,但我没有这个问题
编辑
事实上,在其他计算机上,我无法获得超时异常,但我确实获得了调用监视器外锁的同步异常