I mostly work with winform project. now got change to work with a existing mvc product where i have seen index action which call a function in background thread called ExportToExcel(); i am bit confused it is called with in thread because it take time to
complete. is it right approach?
public ActionResult Index(string user, string password, string ticker, string token, Boolean IsInternal, string type, string preferenceid, string apiKey)
{
Thread t = new Thread
(
() =>
{
ExportToExcel(domain, "");
}
);
// thread background settings
t.IsBackground = true;
// thread name
t.Name = TempData["UserNameT"].ToString();
// thread start
t.Start();
//storing thread into session
TempData["thread"] = t;
// keeping temp data value
TempData.Keep();
}
1) should i use thread Thread t = new Thread() or async/await in index action ?
export to excel take long time. if i use async await then page will take time to render in user pc but if i use
Thread t = new Thread() then how export to excel routine run in different thread so page comes to user pc fast .
i want to know best approach to handle this situation . please advise how to restructure my existing above code to follow best approach.
2) another question that when page render at client side then a button is there in page which download excel fine in client pc. suppose when user click on button then excel file is not generated. so how could i show a message to user
please try after some time, excel file is generating.
or i like to show a loading spinner which detect file is generated, the moment file is generated then file will be start downloading at client pc. how can i do this ? please suggest me a code example to achieve this.
i do not understand what would be advantage to store file name and task in dictionary ?
i could just call ExportToExcel() routine in thread and when excel file generated then store some flag in session variable. i will have a js function at client side which will poll another server side function to know excel file has been generated or not.
if generated then download that excel file and remove that session. does it make sense ?
i could just call ExportToExcel() routine in thread and when excel file generated then store some flag in session variable. i will have a js function at client side which will poll another server side function to know excel file has been generated or not. if
generated then download that excel file and remove that session. does it make sense ?
Your approach will not work. Session does not exist outside the request. The standard solution is setting a key a that identifies the process as suggested (several times above) using a static dictionary. Another very similar approach is using cache.
The benefit with cache is an cached item has a timeout which removes the item from cache automatically.
Sir you said Your approach will not work. Session does not exist outside the request Not very clear.
1) are you saying session will not be available in ExportToExcel() function ?
User Session is loaded in the main thread at the beginning of the request and unload at the end. In other words, the user's session is null once the action returns. The Export would have to finish instantly to have a chance if setting Session which is
unlikely.
TDP
or if we store anything in session from ExportToExcel() function that will not be available ?
Session is null. You easily can test this yourself.
TDP
2) what is point of storing file name and task refernce in static Dict myDict.Add(uniqueName, t) ? reason not clear.
A static dictionary has global scope. It is a very simple method for storing a key/value container. Of course, you'll want to write code that removes the dictionary entry once the process completes. Otherwise the key/value will remain in memory until
the application restarts.
Member
77 Points
150 Posts
How to handle long running operation in mvc Index action like excel file generation
Oct 12, 2020 07:43 AM|TDP|LINK
I mostly work with winform project. now got change to work with a existing mvc product where i have seen index action which call a function in background thread called ExportToExcel(); i am bit confused it is called with in thread because it take time to complete. is it right approach?
1) should i use thread Thread t = new Thread() or async/await in index action ?
await Task.Run(() =>
{
ExportToExcel(domain, "");
});
export to excel take long time. if i use async await then page will take time to render in user pc but if i use Thread t = new Thread() then how export to excel routine run in different thread so page comes to user pc fast .
i want to know best approach to handle this situation . please advise how to restructure my existing above code to follow best approach.
2) another question that when page render at client side then a button is there in page which download excel fine in client pc. suppose when user click on button then excel file is not generated. so how could i show a message to user please try after some time, excel file is generating.
or i like to show a loading spinner which detect file is generated, the moment file is generated then file will be start downloading at client pc. how can i do this ? please suggest me a code example to achieve this.
thanks
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 12, 2020 07:55 AM|ignatandrei|LINK
1 . If I use something for long running work , I will use .NET Core with Background Worker
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1
2. I will use just Tasks, not threads
3. I will have a static Dictionary< unique id ,Task<result of the process> > that I will add the excel thread.
In the action, I will return the unique id to the front end
In the front End I will poll the backend with the unique id to see if the task has been finished
4. Also , you can use SignalR for communicating
Member
77 Points
150 Posts
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 12, 2020 05:19 PM|TDP|LINK
Thanks for the reply. i have no scope to use .net core now. so working with mvc 5.
you said
3. I will have a static Dictionary< unique id ,Task<result of the process> > that I will add the excel thread.
In the action, I will return the unique id to the front end
In the front End I will poll the backend with the unique id to see if the task has been finished
please share some sample code which guide me how to add excel thread in dictionary ?
how to return unique id to the front end?
poll the backend with the unique id to see if the task has been finished
sir please share some sample code which guide me how to construct code for this scenario the way you accomplish. thanks
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 14, 2020 06:21 AM|ignatandrei|LINK
static ConcurrentDictionary<name, Task<whatever return you have from execution> > myDict
public string CreateExcel(){
string uniqueName = < generate unique name for the user and the excel created>
var t = Task.Run( whatever generates the excel)
myDict.Add(uniqueName, t)
return uniqneName;
}
public bool HasTaskFinished(string uniqueName){
// find in myDict if the task for the key uniqueName has finished
}
Member
77 Points
150 Posts
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 14, 2020 07:14 PM|TDP|LINK
Sir Thanks for your reply
i do not understand what would be advantage to store file name and task in dictionary ?
i could just call ExportToExcel() routine in thread and when excel file generated then store some flag in session variable. i will have a js function at client side which will poll another server side function to know excel file has been generated or not. if generated then download that excel file and remove that session. does it make sense ?
please looking for your reply again. thanks
All-Star
53121 Points
23672 Posts
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 14, 2020 07:52 PM|mgebhard|LINK
Your approach will not work. Session does not exist outside the request. The standard solution is setting a key a that identifies the process as suggested (several times above) using a static dictionary. Another very similar approach is using cache. The benefit with cache is an cached item has a timeout which removes the item from cache automatically.
Member
77 Points
150 Posts
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 14, 2020 08:07 PM|TDP|LINK
Sir you said Your approach will not work. Session does not exist outside the request Not very clear.
1) are you saying session will not be available in ExportToExcel() function ?
or if we store anything in session from ExportToExcel() function that will not be available ?
2) what is point of storing file name and task refernce in static Dict myDict.Add(uniqueName, t) ? reason not clear.
please guide. thanks
All-Star
53121 Points
23672 Posts
Re: How to handle long running operation in mvc Index action like excel file generation
Oct 14, 2020 09:08 PM|mgebhard|LINK
User Session is loaded in the main thread at the beginning of the request and unload at the end. In other words, the user's session is null once the action returns. The Export would have to finish instantly to have a chance if setting Session which is unlikely.
Session is null. You easily can test this yourself.
A static dictionary has global scope. It is a very simple method for storing a key/value container. Of course, you'll want to write code that removes the dictionary entry once the process completes. Otherwise the key/value will remain in memory until the application restarts.