テストの目的で、これをページのかみそりブロック内で直接使用してい.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);
}
「こんにちは」というテキストで1秒に1回スパンを書くことを期待していました。「Hello」と書かれることもありますが、毎回、あるいはほとんどの場合ではありません。このタスクが長時間実行されないのはなぜですか?