How to stop long procedure if the client quit

0
0

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

 

  • You must to post comments
0
0

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

  • You must to post comments
0
0

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

  • Julie (ITG)
    So, there are 2 ways to do this: 1. Wisej actually already does this behind the scenes. The client (browser) sends a keepalive message to the server keep the session alive when there is no user activity (user staring at the screen). If the server hasn’t gotten a message from the client in a while (how long the server waits depends on the sessionTimeout variable, which you can set) then the session is terminated. If you want to use this approach, it would be what I already mentioned-you can use the ApplicationExit event or check Applciation.IsTerminated. 2. You can mange your own “keep alive” Use a timer and start when you start printing and stop when printing is done. Then in between prints (as there is no way in js to affect the browser printing) check the time stamp of the last custom keep alive (timer interval event). Timer documentation: https://docs.wisej.com/api/wisej.web/other-components/wisej.web.timer
  • Mirko Bonanno
    Hello Julie, thanks for this answer. “If the server hasn’t gotten a message from the client in a while” is this true event if the server is running a procedure (like printing 2000 reports)? Can the server test the last keepalive sent by the client browser and abort printing if too old ? AFAICS the server will run its procedure to the end before noticing the client quit !?!?
  • Julie (ITG)
    Can the server test the last keepalive sent by the client browser and abort printing if too old ? No. There is no way to test how “old’ the last keepalive is-other than waiting for the ApplicationExit or a similar event to fire. You can assume if that event has not fired, that the session is still active. “The session is still active” meaning that the user has interacted with the application in the last (sessionTimeout*2) seconds.. But if you want a high level of control/access to the time the last keepalive message was sent, you would want to use a timer like I mentioned in my last comment instead of relying on the built in keepalive system. The server will run its procedure to the end before noticing the client quit? – Yes. The client quitting will not stop a SQL procedure running on the server that has already been started.
  • You must to post comments
0
0

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.

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.