How to generate alert on completion of web service with ajax using ASP.NET C#

I am not sure if it is possible to do what I am requesting (have spent some time researching it with no success).

I have an ASP.NET C# page that calls an Ajax function which in turn calls a web service to run a long winded python script. Instead of waiting for the completion of the Ajax call I run the python, show a Toast that the process is running and let the user continue to navigate.

However on the successful completion of the Web service function (post the python run) I would like to alert the user that the process has been complete (irrespective of which page the user has now navigated to). I was interested in calling Toast again from the web service (not sure this is possible), but this is the second prize. The first prize is to send some notification that the process completed successfully (or failed if it did).

Is this even possible or do I need to revert to something like an email on success.

Much appreciated.

1 answer

  • answered 2021-01-17 01:12 Albert D. Kallal

    About the only really "easy" way would be if you are "lucky" and are using say webforms, and you have a master page. For each navagation to a page then the master page load events first. Thus, you have a common area/code stub that runs for every web page on your site.

    So, when that routine is finished, you could say set some session() value, and your code in the master page could thus check session() each time - and if the process is done (you update some flag/setting in session()), then you could inject via the scriptmanger a toast message. The message would only occur when the user navigates, but you could say include in the master page a iframe - and a timer that checks the session value - say every 5 seconds.

    The above is about the only "easy" and practical way. you can also send out (have) push notifications by adopting signalR to your application. The problem with signalR is you do in most cases need "some" JavaScript code in every page - so once again, if you have or are using a master page, then introduction of signalR to your site is a possible (but I do not consider adopting signalR for your site all that great nor easy).

    So, when that long running process is done - it would have to call a web method on your site - and that method would update say that session() value, and then your master page could check that session value say every 5 seconds, and if the "done" flag is set, then it could display a message.


    I think the timer in a master page (with a iFrame) probably is the least amount of work, as signalR is quite a handful to introduce into a project. But if you don't have a common page (say custom menu bar) or location that you can "re-check" that status for all web pages in the application, then you have to inroducte some code into every web page - and that's not all of a practial nor great of a idea. So, if you have a master page - then the timer idea, or perhaps chewing on signlR and introducting that into your web site is a road I would consider.

    I have a case in which a user hits a page, and in that page I have to fetch some PDF documents, and I display PDF "thumb-nails" for the user. The processing time to load the PDF's, and create the thumb nails? (I only create them one time - save them in a sql server). It takes about 5 seconds - maybe 10 if there are say 6 or more documents.

    So, I start a new processor thread to crunch the PDFS. My grid view is in a panel, and I check a session() flag every 1 second. But, once all pdfs thumbs are rendered, then I stop the timer on the form (in that update panel).

    Because of the state-less nature of web pages, the only practical "spot" for transferring the done status was session(). If a timer + session() is not practical for you, then you have to bite the bullet and consider adopting signalR for this. For me, it was quick work to use the timer + session() idea, but using signalR is more preferable for such tasks.