|
当然我们也可以简单的修改上述代码,让把已经处理完成的东西记录下来,类似下面的代码
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder txt = new StringBuilder();
for (int i = 0; i < 100; i++)
{
if (this.Response.IsClientConnected)
{
txt.AppendLine();
txt.AppendLine(DateTime.Now.ToString("u"));
txt.Append("********** ");
txt.AppendLine(i.ToString());
Response.Write(DateTime.Now.ToString("u"));
Response.Write("\r\n");
Thread.Sleep(500);
}
else
{
break;
}
}
txt.AppendLine(DateTime.Now.ToString("u"));
Response.Write(DateTime.Now.ToString("u"));
Response.Write("\r\n");
// 把一些信息写到另外一个文件,借此察看是否正在运行
string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
DateTime dt = DateTime.Now;
string shortfileName = string.Format("errors_{0:0000}{1:00}{2:00}.log", dt.Year, dt.Month, dt.Day);
string fileName = Path.Combine(dir, shortfileName);
StreamWriter sw;
if (File.Exists(fileName))
sw = File.AppendText(fileName);
else
sw = File.CreateText(fileName);
sw.Write(txt.ToString());
sw.Close();
sw = null;
}
需要注意的是, 使用isClientConnected是要占用一定的系统资源的。
isClientConnected 实际上需要向客户端输出一点东西,然后才知道客户端是否仍然在线。
这样,除非你的应用非常耗时,否则建议你不要用 isClientConnected 。 免得判断 isClientConnected 使用的资源比你实际业务逻辑使用的资源还要多。
在任何情况下, Response.IsClientConnected 都要有些开销,所以,只有在执行至少要用 500 毫秒(如果想维持每秒几十页的吞吐量,这是一个很长的时间了)的操作前才使用它。作为通常的规则,不要在紧密循环的每次迭代中调用它,例如当绘制表中的行,可能每 20 行或每 50 行调用一次。
共2页: 上一页 [1] 2 下一页
|