public class HomeController : AsyncController
{
public void ArticleAsync(string name)
{
AsyncManager.OutstandingOperations.Increment();
Task.Factory.StartNew(() =>
{
string path = ControllerContext.HttpContext.Server.MapPath(string.Format(@"articles{0}.html", name));
using (StreamReader reader = new StreamReader(path))
{
AsyncManager.Parameters["content"] = reader.ReadToEnd();
}
AsyncManager.OutstandingOperations.Decrement();
});
}
public ActionResult ArticleCompleted(string content)
{
return Content(content);
}
}
public class HomeController AsyncController
{
public Task<ActionResult> Article(string name)
{
return Task.Factory.StartNew(() =>
{
string path = ControllerContext.HttpContext.Server.MapPath(string.Format(@"articles{0}.html", name));
using (StreamReader reader = new StreamReader(path))
{
AsyncManager.Parameters["content"] = reader.ReadToEnd();
}
}).ContinueWith<ActionResult>(task =>
{
string content = (string)AsyncManager.Parameters["content"];
return Content(content);
});
}
}
public class HomeController AsyncController
{
public Task<ActionResult> Article(string name)
{
return Task.Factory.StartNew(() =>
{
string path = ControllerContext.HttpContext.Server.MapPath(string.Format(@"articles{0}.html", name));
using (StreamReader reader = new StreamReader(path))
{
return reader.ReadToEnd();
}
}).ContinueWith<ActionResult>(task =>
{
return Content((string)task.Result);
});
}
}
public class AsyncManager
{
public AsyncManager();
public AsyncManager(SynchronizationContext syncContext);
public EventHandler Finished;
public virtual void Finish();
public virtual void Sync(Action action);
public OperationCounter OutstandingOperations { get; }
public IDictionary<string, object> Parameters { get; }
public int Timeout { get; set; }
}
public sealed class OperationCounter
{
public event EventHandler Completed;
public int Increment();
public int Increment(int value);
public int Decrement();
public int Decrement(int value);
public int Count { get; }
}
AsyncManager.OutstandingOperations.Increment(3);
Task.Factory.StartNew(() =>
{
//异步操作1
AsyncManager.OutstandingOperations.Decrement();
});
Task.Factory.StartNew(() =>
{
//异步操作2
AsyncManager.OutstandingOperations.Decrement();
});
Task.Factory.StartNew(() =>
{
//异步操作3
AsyncManager.OutstandingOperations.Decrement();
});
public abstract class Controller ControllerBase, IAsyncManagerContainer,...
{
public AsyncManager AsyncManager { get; }
}
public interface IAsyncManagerContainer
{
AsyncManager AsyncManager { get; }
}
AsyncManager.OutstandingOperations.Increment(3);
Task.Factory.StartNew(() =>
{
//异步操作1
AsyncManager.Finish();
});
Task.Factory.StartNew(() =>
{
//异步操作2
AsyncManager.Finish();
});
Task.Factory.StartNew(() =>
{
//异步操作3
AsyncManager.Finish();
});
public class HomeController AsyncController
{
public void ArticleAsync(string name)
{
//AsyncManager.OutstandingOperations.Increment();
Task.Factory.StartNew(() =>
{
string path = ControllerContext.HttpContext.Server.MapPath(string.Format(@"articles{0}.html", name));
using (StreamReader reader = new StreamReader(path))
{
AsyncManager.Parameters["content"] = reader.ReadToEnd();
}
//AsyncManager.OutstandingOperations.Decrement();
});
}
public ActionResult ArticleCompleted(string content)
{
return Content(content);
}
}
public class HomeController AsyncController
{
public void XxxAsync(string name)
{
Task.Factory.StartNew(() =>
{
AsyncManager.OutstandingOperations.Increment();
//...
AsyncManager.OutstandingOperations.Decrement();
});
}
//其他成员
}
public class HomeController AsyncController
{
public void XxxAsync(string name)
{
AsyncManager.OutstandingOperations.Increment();
Task.Factory.StartNew(() =>
{
//...
AsyncManager.OutstandingOperations.Decrement();
});
}
//其他成员
}
public class HomeController AsyncController
{
public Task<ActionResult> Data()
{
return Task.Factory.StartNew(() =>
{
while (true)
{ }
return GetModel();
}).ContinueWith<ActionResult>(task =>
{
object model = task.Result;
return View(task.Result);
});
}
//其他成员
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]
public class AsyncTimeoutAttribute ActionFilterAttribute
{
public AsyncTimeoutAttribute(int duration);
public override void OnActionExecuting(ActionExecutingContext filterContext);
public int Duration { get; }
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited=true, AllowMultiple=false)]
public sealed class NoAsyncTimeoutAttribute AsyncTimeoutAttribute
{
// Methods
public NoAsyncTimeoutAttribute() base(-1)
{
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有