shutDown Functions

When MyJob starts, it runs the ExecuteStartupProgram, which invokes the startup program. When this program returns to ExecuteStartupProgram, it then returns to its caller, which in turn proceeds to end the Job and destroy the thread running the job. You can also decide to end the job programmatically in the procedural code or from the browser.

Programmatically you can use the RequestShutdown or the Shutdown method of WebJob class to shut down the job. This method ends all active programs, closes the database connections for disk and printer files, and then aborts the thread assigned to the job.

From the browser you can use JavaScript to call the Shutdown function available that requests, in an out-of-band call, the !EOJ.aspx page that abandons the ASP.NET session causing the job to be shut down.

There are two javascript functions that are used for control of a client-side shutdown to provide some programming cleanup if needed. They are onBrowserClose() and isBrowserClosing().

onBrowserClose()

onBrowserClose() will check for the existence of function isBrowserClosing() and its return value to see if a client-side shutdown has been requested. If the return value is true, the function will call Shutdown terminating the Job on the server, while a return value of false will prevent the shutdown. Be aware that onBrowserClose() will do nothing if isBrowserClosing() is not defined. If the call does not occur, the user's job will remain active until the time limit specified in the session timeout property is reached.

isBrowserClosing()

isBrowserClosing() can be used to perform any checks and cleanup before returning True to request the shutdown or False to prevent the shutdown.

Example 1

In this example, an action is required on the part of the user to initiate the shutdown on the client side when there are certain conditions or cleanup that needs to occur before the shutdown can occurs. This removes the user's ability to use the browser's Close button.

Verify that the body tag does NOT have OnUnload specified:

            <body>
          

Add the JavaScript function isBrowserClosing() to the display file inside the <head> or <body> tag similar to the following:

            <scriptlanguage="javascript" type="text/javascript">
 {         
   functionisBrowserClosing()   
      // programmer can perform desired checks and/or cleanup herereturn true;  
      // request shutdown
 }  
</script>

In the body section, you then attach onBrowserClose to the onClientClick event of a control like those shown below for a button that the user presses to initiate the shutdown.

            <asp:ButtonID="Btn1 runat="server" onClientClick="onBrowserClose()" Text="Bye" /> 

Example 2

Similar to the above, this example uses the isBrowserClosing() function as the onClientClick event for the user control button for the shutdown and bypasses onBrowserClose(). This also removes the user's ability to use the browser's close button.

As above, the <body> tag must NOT have onunload specified. Add the JavaScript function isBrowserClosing() to the display file manually inside the <head> or <body> tag similar to the following:

            <scriptlanguage="javascript" type="text/javascript">
{     
      functionisBrowserClosing()     
      // programmer can perform desired checks and/or cleanup hereShutdown();
}
</script>

In the body section, set the onClientClick event to isBrowserClosing for the button that the user presses to initiate the shutdown:

            <asp:ButtonID="Btn1" runat="server" onClientClick="isBrowserClosing()" Text="Bye" />
          

Example 3

This example shows how to control the shutdown if you need to retain the early Monarch behaviors that allows the session to terminate when the user clicks on the browser's close button.

Ensure the body tag has onunload as shown here:

            <bodyonunload="onBrowserClose();" >
          

Then place the following within the head or body section:

            <
            script
            language
            ="javascript"
            type ="text/javascript">
   // Allow user to use Browser's close button to end sessionfunctionisBrowserClosing()
  {
       If(event && event.clientX < 0 && event.clientY < 0)
          // programmer can perform desired checks and/or cleanup herereturn true;   
          // do shutdownelse
          return false;  
        // do not shutdown
 }
</script>