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 this possible?
I tried System.Threading
, but it didn't work. I used threading in C#, WPF, but in ASP.NET Core MVC, it's different.
I don't know how to use it and how it's working.
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 this possible?
It would be nicer, if you could share your real scenario and what exactly you have tried and not worked as expected. So regarding the possibility, it depends on the context, scenario or the requirement.
Unllike WPF, in ASP.NET Core 6 MVC, we usually don't use manual threading for data upload more specifically bulk operation!
Instead we use async/await
for asynchronous I/O-bound tasks like database saves. For CPU-bound work, consider offloading to background threads with Task.Run
or ThreadPool
, but avoid blocking the main thread.
On the other hand, for complex tasks, explore background services or libraries like Hangfire. Remember user experience with progress indicators and error handling, and test thoroughly for a smooth upload experience without waiting.
Like I said, you could consider uploading method as asyncronous operatiion, for instanec:
public async Task<IActionResult> UploadData()
{
var uploadTask = Task.Run(async () =>
{
await _dataService.UploadToDatabase(data);
});
return View("UploadStarted");
}
Note: As you can see we can initiate upload process asynchronously. However, if you think this doesn't meet your requirement, then you can consider background worker service or Hangfire. Then again, its completely depends on the scenario and requirement. If you still want to study more, please refer to this official document. Alternatively, you can post new question based on your scenario along with reproducible sample what you are struggling with.