出于测试目的,我直接在.cshtml
页面的剃刀块内使用它。
@functions{
public class Inline
{
public HttpResponseBase r { get; set; }
public int Id { get; set; }
public List<System.Threading.Tasks.Task> tasks = new List<System.Threading.Tasks.Task>();
public void Writer(HttpResponseBase response)
{
this.r = response;
tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(
() =>
{
while (true)
{
r.Write("<span>Hello</span>");
System.Threading.Thread.Sleep(1000);
}
}
));
}
}
}
@{
var inL = new Inline();
inL.Writer(Response);
}
我曾期望它每秒写一次带有文本“Hello”的跨度。它有时会写一次“Hello”,但不是每次甚至大部分时间。为什么这个任务没有长时间运行?