C# 在其他代码运行时从函数返回响应。netcore
我有这个功能:C# 在其他代码运行时从函数返回响应。netcore,c#,.net-core,C#,.net Core,我有这个功能: [HttpGet] [Route("See/{OID}")] public IActionResult See([FromRoute]long OID) { //time ham bayad chek bshe orders order = _context.orders.FirstOrDefault(e => e.OID == OID); lastViewed lv = _context.lastV
[HttpGet]
[Route("See/{OID}")]
public IActionResult See([FromRoute]long OID)
{
//time ham bayad chek bshe
orders order = _context.orders.FirstOrDefault(e => e.OID == OID);
lastViewed lv = _context.lastViewed.FirstOrDefault(e => e.UID.ToString() == User.Identity.Name);
if (DateTime.Compare(lv.sentTime.AddSeconds(order.seconds), GetUTCDateTime()) < 0)
return Content("1");
unViewed UV = _context.unViewed.FirstOrDefault(e => e.UID.ToString() == User.Identity.Name && e.OID == OID);
if (UV != null)
_context.unViewed.Remove(UV);
coins coin = _context.coins.FirstOrDefault(e => e.UID.ToString() == User.Identity.Name);
if (order.type > 50)
coin.Coin += order.seconds * 8;
else
coin.Coin += order.seconds;
order.view--;
if (order.view == 0)
{
var uv = _context.unViewed.Where(e => e.OID == OID);
foreach (unViewed uvv in uv)
_context.unViewed.Remove(uvv);
pastOrders po = new pastOrders
{
link = order.link,
OID = order.OID,
UID = order.UID
};
_context.pastOrders.Add(po);
_context.orders.Remove(order);
}
_context.SaveChanges();
return showlink();
}
[HttpGet]
[路由(“See/{OID}”)]
公共IActionResult请参见([FromRoute]长OID)
{
//时代周刊
orders order=\u context.orders.FirstOrDefault(e=>e.OID==OID);
lastViewed lv=\u context.lastViewed.FirstOrDefault(e=>e.UID.ToString()==User.Identity.Name);
if(DateTime.Compare(lv.sentTime.AddSeconds(order.seconds),GetUTCDateTime())小于0)
返回内容(“1”);
unViewed UV=\u context.unViewed.FirstOrDefault(e=>e.UID.ToString()==User.Identity.Name&&e.OID==OID);
如果(UV!=null)
_上下文。未查看。删除(UV);
coins coin=\u context.coins.FirstOrDefault(e=>e.UID.ToString()==User.Identity.Name);
如果(订单类型>50)
硬币。硬币+=订单。秒*8;
其他的
coin.coin+=order.seconds;
order.view--;
如果(order.view==0)
{
var uv=_context.unViewed.Where(e=>e.OID==OID);
foreach(uv中未观察到的uvv)
_上下文。未查看。删除(uvv);
pastOrders po=新的pastOrders
{
link=order.link,
OID=order.OID,
UID=order.UID
};
_context.pastOrders.Add(po);
_context.orders.Remove(订单);
}
_SaveChanges();
返回showlink();
}
showlink()函数是IActionResult。showlink()与其他代码完全独立。我的问题是如何首先向用户响应showlink(),然后运行其他代码?
非常感谢。您可以安排代码的其余部分在单独的线程上运行。但是请注意,这并不是完全安全的,因为您没有检查线程是否正确执行。为了做到这一点,您需要在代码中添加其他检查,以及在出现故障时得到通知的方法。采用异步模型是一项艰巨的任务,也是长期的解决方案 但是,对于此特定情况,以下内容应解除对您的阻止:
var result = showlink();
Task.Factory.StartNew(() => {
// Rest of your code that needs to run in the background.
});
return result;