How do I close a splash message after generating a file? - c#

I've run into a rather sticky situation and I was hoping you all could help. As part of my application, I'm generating a file for my users. Unfortunately, the time it takes to generate this file could be close to 5 minutes. In order to appease my users, I'm showing a message asking them to please wait. Once I have the file generated, I want to return the file to them and clear the message. I'm using the ASP.NET timer to check when the file has finished generating.
My problem comes once the file has finished generating. At the point, I need to do three things:
Pass the file to the user.
Close the message.
Disable the Timer.
My problem comes from the fact that once I've finished writing the file to the response, my postback doesn't finish, so the Viewstate doesn't get updated, so the message and Timer are still there.
Any ideas?

I think you need to break this down into several stages instead of doing all your processing in a single page request. If I were designing this I'd look to do something like this..
User initiates creation of file by clicking some link on your site. This writes some info to a DB table, or otherwise kicks off a process that generates the file.
User is presented with a "please wait" page that can update itself every N seconds.
When file is generated the DB table is updated with the status of "ready".
The "please wait" page refreshes, the job is seen to be complete, the file is now downloaded.
Assuming the "please wait" page uses some identification based upon an authenticated user then this has the benefit of allowing the user to collect their file regardless of whether they get bored and/or accidentally close their browser.
In fact, why not check out these SO posts which cover similar ground...
Handling Long Running Reports
Long-running code within asp.net process
BackgroundWorker thread in ASP.NET
... and there are others (search for "ASP.NET long running")

I've done similar things but without using the timer. Basically I launch a dialog box OnClientClick and then let the server side to it's thing (OnClick). I use the following javascript to handle the postback returning in the OnClientClick:
Dialog.show();
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
function(sender, args) {
MyDialog.hide();
});

The KISS method is redirecting to a loading page (with loading message etc.) which in turn calls the slow loading resource. The page churns away showing the loading message until the resource is ready. The only downside is that if your resource really does take 5 minutes to generate, the request will time out. If this is the case, #Martin-Peck has a better solution already posted.

Related

c# runing code without blocking navigation

I'm building an application that generates excel files in asp .net core 2.2.
The excel generation may takes some time depending on the user's needs. (like sometimes, it's up to 20-30s).
My question is : how can I run this generation without blocking user's navigation?
Like, the user could starts the file generation, then continue his navigation on website and come back later to download the generated files. What would be the way to do so?
Run generation in a background task. generate name of the excel file and save the relation either in DB or in tokken (depends on your needs). when the thread will generate excel file update status in db. you can notify user or let user see the info on specific page. there are a lot more ways to do it tho...
Not much a technical, c#-specific question, but more like a UX problem.
You could leave a button the user can click on to request the generation of said files. You run a service that receives such requests, and start processing.
When the service starts the generation, the page simply shows a message saying "Please wait, generating...".
When it finishes, change the page content to a link that allows the user to download it.
In JS you can open a new tab, and you will be processing the excel file in this new tab.
Or, you can create the excel file on the serveur (displaying that its's currently being created), without blocking the navigation, and when your user comes back to the generating page, if a file is found, you can make it downloadable.

IE file save/saveas/cancel dialog, is there any way to find out they selected Cancel?

This is C#, asp.net. My page does a response write to a file for downloading. I need to know if the user actually selected open or save because we raise an event to the effect that the person has downloaded the file, whether they did an open or save. But we don't want to raise that event if they hit cancel. Is something like this possible?
No, you can't tell if they pressed Cancel or anything else. That's not an interaction with your web page. Your web page has done its part, providing the response to the user. Now the user is interacting with the browser, telling the browser whether or not they want to accept the download.
To make a comparison: Knowing whether or not they download the file would be similar to knowing what they name the file or what folder they save it in. All of those are behaviors between the user and the browser, not between the user and your application running in the browser.

Will a long running .net process be termintated if the user session is terminated?

I have a ASP C# application that allows users to upload and process text files. after the upload, the parser is called and starts its work. depending on the size and quantity of the files the user uploads, this process can run for up to 20 hours. If my users navigate away from the page, will that terminate the text processing? If so, is there a way to keep it running after the user leaves? I would like to setup some functionality to let them know when its complete.
I'd say: Start the text file processing in a separate thread, that way you won't have to consider things like the regular http request timeout (default 20 minutes) etc.
Regarding feedback to the users, do you expect them to stick around for 20 hours looking at the browser? If not, perhaps you could send them an email when the processing is completed? Otherwise you could implement some form of polling which allows the web browser to check back every (n) minutes to see if the processing of their text file has been completed.

Does ASP.NET close streams automatically upon navigating to a different page?

If my web application is in the middle of reading a large file, and the user navigates to a different page, what happens to the execution of the previous page? Does it complete, or is the page and the open file steam flushed and closed?
Chances are, your page will continue to process the file until it's time to write the output to the client, at which time it will abort.
What kind of app is it? Windows Forms? MVC? The app won't know the client is no longer there until it has to write to the stream.
I agree with Erik.
Once you have submitted the request, it will process it and then when its time to write to the output client, it will abort it.

Web form handling script to execute background jobs on server then send an email when ready

I am developing an application in C# ASP.NET to allow users to customise a number of graphics templates with their company logo.
Typically,
User uploads their company logo.
User selects a number of templates (through a HTML form) of different file types for customising.
My web application will accept the user's request, go through each individual item requested and determine its file type. It will then call the appropriate module depending on the file type (e.g. to customise a PDF) and finally insert the logo in the template. These steps are repeated for each file requested.
Once all requested templates for a user are generated, they are grouped together in a zip file and a link for downloading is sent via email.
I would like some advice on how best to accept the user's requests and process the files in ASP.NET.
One way of doing this is to keep the user waiting until all files are generated, therefore until the form handling script would have completed its execution. I reckon this is likely to trigger script timeout errors quite easily for requests that take long to be processed (large number of templates requested or sizable number of concurrent users), and as such is not a very efficient solution.
Another option would be to register the user's request, redirect him/her to another page immediately after (explaining that an email will be sent shortly with a download link), and then proceed to process the files on the server using some background job or similar without the risk of script timeouts. An email is sent when all files for that user are generated.
I am familiar with web application development but this is one of my first forays into .NET development so your help is greatly appreciated.
How can I implement the second option in C#?
It sounds like what you want is a web site which accepts the user input and then passes control to an offline process (such as a Windows Service) to perform the more intensive tasks asynchronously from the site, allowing the user to continue using the site (or go do something else) while processing takes place. Something like this:
User "initiates" a "batch" on the website. Whether it's setting up some values, uploading some kind of batch of things to process, etc. is entirely up to what you're doing. The main point is that the user starts it off.
The necessary information for the process to do its magic is persisted to the database and the user is told that the process has been queued for processing. The user can now go about doing other things.
The back-end application (or multiple applications) polls the database periodically (every minute, every 5 minutes, etc.) or, in the case of an uploaded "batch" file could use something like a FileSystemWatcher, to look for new things to do and does them. Use multi-threading or whatever you need to make that happen, but the main point is that it's an "offline" process that the website isn't waiting on.
When the process completes, some flag is set in the database (on the record being processed, or maybe a "message" record in the user's "inbox" or whatever) indicating that the process is done.
The website UI has some indicator which, any time it's loaded, checks for the aforementioned flag(s) to indicate to the user that a queued process has been completed and is ready to be viewed.
So, essentially, you have a single database accessed by two applications (your web application and your Windows Service (or console app run by a task scheduler, etc.).
Is that basically what you're looking for? I feel like I could be more specific, do you have any specific concerns about the setup?

Categories