C
C#10mo ago
John

.NET CORE 6 MVC Threading

When you upload data into your database, it takes too much time to wait. So I want to navigate to other pages while uploading. Is it possible?
14 Replies
jcotton42
jcotton4210mo ago
How long are we talking?
Cracker
Cracker10mo ago
for long running tasks see TaskCreationOptions.LongRunning https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md if its just milliseconds or seconds then just use like Task.Run without await
Jimmacle
Jimmacle10mo ago
you mean leave the page that is currently submitting a file to your website? that's what tabs are for when
Cracker
Cracker10mo ago
TaskCreationOptions.LongRunning is not recommended if that code block will be triggered too often, cause it might creates new thread for each
Jimmacle
Jimmacle10mo ago
the solution kind of depends on more than is being stated here if you want the "upload" to finish faster, maybe receive the file first then process it into the DB in the background
jcotton42
jcotton4210mo ago
Note your need to be careful here If you're using anything injected it will become invalid after the request ends
Jimmacle
Jimmacle10mo ago
i don't think you can actually navigate away while the browser is making the request with the data anyway
Cracker
Cracker10mo ago
if its like a form input then yes but once request reached to controller, other operastions can be done at background and send response immediate if it takes time to upload database then we are lucky, otherwise have to wait form input to complete
John
JohnOP10mo ago
Yes exactly. This is what I want Sometimes, the excel file contains more than 50000rows and it takes about 10+minutes. Can I use TaskCreationOptions.LongRunning
Jimmacle
Jimmacle10mo ago
i suggest using hangfire
John
JohnOP10mo ago
Could you explain in more detail? cuz I have no experience.
Jimmacle
Jimmacle10mo ago
Hangfire – Background Jobs for .NET and .NET Core
An easy way to perform fire-and-forget, delayed and recurring tasks in ASP.NET applications. No Windows Service required, backed by persistent storage.
Jimmacle
Jimmacle10mo ago
it's a background job scheduler
John
JohnOP10mo ago
thnx
Want results from more Discord servers?
Add your server