Hello,
I have a long printing procedure running with PPJ using CrystalReport . How can I detect that the client quit and abort the printing procedure before the end ?
TIA
Mirko
So, there are two things you need to do here:
1. Detect that the client quit
There is absolutely no way to specifically detect a browser being closed in javascript. Closing the browser tab, closing the entire browser, navigating away, or shutting down the computer are identical operations as far as the server is concerned. However, I would say all of these fall under “the client quit”, so it should work just fine.
You can detect when the client quits by attaching an event handler to the ApplicationExit event.
Note that ApplicationExit will fire when the session expires, which will happen after sessionTimeout * 2 (in default.json). If you have a high session timeout then it may take long before the session expires.
I’ve attached a sample using Wisej that creates a file (log.txt) when the ApplicationExit event fires. (Note that the sample uses Wisej, but you can also use ApplicationExit in PPJ Web). Also note that if you run the sample from Visual Studio it won’t work. The ApplicationExit event fires when you close the browser. But when running via Visual Studio, the program stops running when you close the browser and thus the code in the ApplicationExit event handler does not run. If you deploy the test application to a server (for example, IIS) then you will notice that the log file is generated shortly after the browser is closed. The server continues running after the browser is closed and that’s how it’s able to run the code in the ApplicationExit event handler. In the sample, I’ve set the session timeout to 2 seconds (via default.json), so the log file will be generated pretty quickly.
You may also find it helpful to read the documentation on session management: https://docs.wisej.com/docs/concepts/session-management
2. Run some code to abort the printing procedure.
In your case, you don’t want to create a text file on ApplicationExit, you want to abort a printing procedure.
To figure that out, you’ll need to refer to the Crystal Reports documentation, as that’s a third party software not developed by us: https://help.sap.com/docs/SAP_CRYSTAL_REPORTS
I’m (the application is) actually printing (to pdf and then zipping and then downloading) about 2000 reports. I would like, after each report, to ensure the client did not quit and printing the next one only in this condition. Can the client update a token on the server each x seconds (via javascript ?) so that the server could test the existence of it and abort the long printing operation if not ?
TIA
Mirko
You can inject a JavaScript EventListener to manage the ‘beforeunload’ event into your page (or window o desktop) using the InitScript property of page/window/desktop
window.addEventListener(‘beforeunload’, function (e) {
App.ManageBrowserClose();
});
The listener call a method inside the Program Class ( Program.cs) of you Application. In this example the method il called ManageBrowserClose()
The called method is decorated with the [WebMethod] attribute so it became callable from JavaScript.
[WebMethod]
public static void ManageBrowserClose()
{
try
{
//Here you code
}
catch (Exception)
{
throw;
}
}
Be aware that you can’t debug in Visual Studio the code when invoked by the EventListener because Visual Studio lost connection with the browser, but the code inside is executed.
Please login first to submit.