I am currently working on building a scanning portion of an app, and noticed that when my eventlistener hits and executes a function, I eventually get the following error:
A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.
After doing some research here, I learned that I should probably be awaiting those processes, but what I've added doesn't seem to be doing the trick. My threads are still getting tangled. My console shows the issue occuring specifically at:
GetByBarcode(String barcode)
But I'm not sure where else I would need to account for an await operator in that task. Am I missing an await call somewhere, or could the issue actually big bigger than that? Perhaps I've misunderstood await. I've added the relevant portions of code below.
<div @onkeydown="@KeyDown"
tabindex="0"
@ref="scanner">
public async Task KeyDown(KeyboardEventArgs e)
{
barcode += e.Key;
await CheckBarcode();
}
private async Task CheckBarcode()
{
testStuff = await stuffRepository.GetByBarcode(barcode);
if(testStuff is null)
{
//
}
else
{
await stuffRepository.Update(testStuff, loginID);
await LoadStuff();
barcode = "";
}
}
public async Task<StuffDTO> GetByBarcode(string barcode)
{
var obj = await _db.StuffTable.FirstOrDefaultAsync(u => u.Barcode == barcode);
if (obj != null)
{
return _mapper.Map<Stuff, StuffDTO>(obj);
}
return new StuffDTO();
}
KeyDowncall? I assume youre doing something like@onkeydown="OnKeyDown". If so, then it wants to be@onkeydown="async (e) => await OnKeyDown(e)".