2

我将以下代码用于网页的关键部分

if(Monitor.TryEnter(lockObj,60000))
{
  try{
       //write some things to a file
     }
  finally
  {
    Monitor.Exit(lockObj);
  }
}

在这里,lockObj 是类的静态成员。我的问题是,如果用户在执行关键部分时关闭网页或浏览器会发生什么?lockObj 是否对未来的页面请求保持锁定状态?

4

2 回答 2

3

Nothing automatically occurs when the user closes a browser window or navigates to another page, if the request is still processing. This is why the HttpResponse.IsClientConnected property exists - so your code can perform appropriate checks as and when you can do something useful.

If a timeout occurs on the server side, then the finally block should operate at around that time and the lock will be released. Whether it is safe for the lock to be released at this time is something only you can determine - the lock must have been put there for a reason, and if a timeout occurs, that may mean that the (shared, lock requiring state) isn't stable for whoever next acquires the lock.

But these are two very different sets of issues.

于 2012-11-05T11:07:17.890 回答
0

如果你设法杀死里面的执行线程

try {
  //write some things to a file
}

你可能有一些问题。但是对于所有有例外的实际情况,代码都会起作用。

实际上,只是避免Thread.Abort.

于 2012-11-05T11:00:42.530 回答