这是一个 .NET Razor 应用程序。我在 CSHTML 中对 onpost 方法进行了 AJAX 调用。OnPost 执行了一些 DB 操作,然后调用了我想在其自己的线程中运行的方法。如果我等待该方法,一切就会正常,但我需要它继续执行其操作并让用户继续使用表单。它总是在 _context2.Attach(mo).State EntityState.Modified; 处失败,错误为无法访问已处置的对象。此错误的常见原因是处置上下文。我尝试过任务/线程,将返回类型更改为 IactionResult 等。总是出现同样的问题。抱歉,C# 中的线程处理还不熟悉。有什么想法吗
public async Task<JsonResult> OnPostSignup(MarketOperator MarketOperator, Boolean Confirm = false)
{
Does some DB stuff
_context.Attach(RAExists).State = EntityState.Modified;
await _context.SaveChangesAsync();
//Task.Run(async () => AI_Creation(_context,MarketOperator.MarketDescription,
//MarketOperator.MOID));
Thread t3 = new Thread(() => AI_Creation(_context,
MarketOperator.MarketDescription, MarketOperator.MOID));
t3.Start();
}
public async Task<IActionResult> AI_Creation(AppDbContext context,string idea, int moid)
{
//calls a bunch of open-ai calls which are awaited.. These work
enter code here
AppDbContext _context2 = context;
MarketOperator mo = await _context2.MarketOperator.FirstOrDefaultAsync(ra => ra.MOID == moid);
mo.Keywords = aI_Answers.keywords;
_context2.Attach(mo).State = EntityState.Modified;
await _context2.SaveChangesAsync()
}