Click here to Skip to main content
13,900,622 members
Rate this:
 
Please Sign up or sign in to vote.
I Have Two Menu Items in My Mvc Application, I want to Allow user to navigate second menu when he clicks while the current(first) action is getting executed, First action is asynchronous action but still its blocking user until it gets completed

What I have tried:

I blocked user not go into other action while current is getting executed but my requirement is I should allow the user to go into other pages while current one being executing
Posted
Updated 14-Mar-19 9:17am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

That's not quite how HTTP works. HTTP works on the Request AND Response. While you can navigate away from the original URL, any requests it made are likely to be canceled.

The best thing I could recommend is to have URL1 on the server just a "wrapper" which defines a task and then runs it. Once Task.Run() starts you can then return the proper response (should be 200: Accepted).
This should satisfy your AJAX request almost immediately so that the second link can be acted upon

Reference:
c# - Return before async Task complete - Stack Overflow[^]
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

This is a repost of your question, Display visual result of async process in MVC action[^]. The content from your comment helps in understanding the question.

The problem is that your tasks are asynchronous, meaning there is no information as to how much time it will take. Thus, it is a good practice to use a background service to keep a track of that task and you show the progress in the layout. This way, you will decouple the current page and the task.

You can utilize background threads to perform these actions. Thread pool threads are limited, and you will run out of threads quickly. Creating new threads is expensive and costly, and might not be a good solution at all. In this way, if I had to do this, I would consider using some sort of event-processing, like a message queue to manage and maintain the state of certain events and operations. Each operation can have its state, progress, and I can use that to update the users.

Check these links for a practical overview,
How to run Background Tasks in ASP.NET - Scott Hanselman[^]
c# - How to run long-lasting process asynchronously under asp.net? - Stack Overflow[^]
Simple Asynchronous Background Task Processing with Progress Dialog (Silverlight Solution)[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190306.1 | Last Updated 14 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100