ScriptX logotype
  • Getting started
  • Documentation
  • Samples
  • Pricing
Free Trial Downloads
  • Home
  • Getting started
  • Documentation
  • Samples
  • Pricing
Developers ›  Knowledge Bank ›  How To Guides ›  ScriptX.Services ›  Then To Now ›  Stage 7

WaitForSpoolingComplete()

This walk-through illustrates taking a classic document that has a controlled print experience and works only in Internet Explorer and making it work with ScriptX.Services in any browser on any platform using the minimum amount of development effort and making as few changes as possible (though more changes might be desirable).

Previously : Stage 6 - Preparing for advanced uses

Background

Each of the functions Print, PrintHTML and BatchPrintPDF will return immediately after the print request has been started and/or queued with the print continuing either in the background with ScriptX.Add or with ScriptX.Services at the server.

The browser remains responsive and so the user is able close the tab/window while the print is in progress.

ScriptX.Addon

With ScriptX.Addon, if the window/tab is closed or navigated away from then any print in progress would immediately stop.

So, if a navigation occurs or window/tab is closed while printing with ScriptX.Addon, ScriptX.Addon will pause the action for 5 seconds to allow the print to complete.

If the print is still not complete then the user will be asked if they wish to abandon the print (the window/tab will close) or wait for it to complete before continuing the action. The prompts that appear are clear and concise and can be customised.

ScriptX.Services

No such solution is available to javascript.

The browser beforeunload event can be used to some effect but suffers from the fact that the dialog message is misleading as only a generic message will ever be displayed and there is no way to offer users more sophisticated options other than "stay or leave".

This means there is no satisfactory way to implement the behaviour of ScriptX.Add-on with ScriptX.Services.

There is an extent to which this does not matter as printing is occuring in a separate service and will continue after the navigation or closure of a tab/window.

However, there are occasions where finer control and feedback to the user is worthwhile. For example:

  • A number of jobs have been submitted to the printHtml() API. If the window/tab is closed many prompts may be given to the user to request permission to close the window.
  • Jobs submitted to printHtml() should be verified that they have succeeded and re-submit/report those that fail for some reason.
  • Visually show printing is in progress and the user should wait.
  • Cleanly avoiding an error display if a print is started with ScriptX.Servicees and the window is closed immediately after.

Each of these use cases is handled by the WaitForSpoolingComplete API.

The WaitForSpoolingComplete() API Is emulated with the ScriptX.Services client library but requires an additional argument. To ease the transition the MeadCoScriptXJS library implements a function that returns a promise and can be used with either ScriptX.Addon or ScriptX.Services.

This means this code:


factory.printing.Print(false);
factory.printing.WaitForSpoolingComplete();
self.close();

becomes:


factory.printing.Print(false);
MeadCo.ScriptX.WaitForSpoolingComplete().finally(function() {
    self.close();
})

Or, in a modern browser where await can be used with a Promise then it is a very simple change:


factory.printing.Print(false);
await MeadCo.ScriptX.WaitForSpoolingComplete();
self.close();

For a more complete example:


$("#btn-print").click(function () {
    MeadCo.ScriptX.PrintPage2(true).then(function (bStarted) {
        if (bStarted) {
            var $dlg = $("#dlg-busy");

            $dlg.modal("show");
            MeadCo.ScriptX.WaitForSpoolingComplete().finally(function () {
                $dlg.modal("hide");
            });
        }
    });
});

Or, for a more complete example where code will only run in a modern browser where await can be used with a Promise:


$("#btn-print").click(function () {
    if ( await MeadCo.ScriptX.PrintPage2(true) ) {
        var $dlg = $("#dlg-busy");

        $dlg.modal("show");
        await MeadCo.ScriptX.WaitForSpoolingComplete();
        $dlg.modal("hide");

    }
});

Open your browser (any browser) and go to the address  https://scriptxprintsamples.meadroid.com/ThenToNow/now-stage7

A more extensive discussion of WaitForSpoolingComplete() is available at ScriptX Samples

Next up: Stage 8 - Recommendations for some common issues.
  • Knowledge Bank
  • 'How To' Guides
    • ScriptX.Services
      • Introduction
      • Getting started
      • Evaluate with modern code
      • Maintaining investment in current code
        • Stage 1: Adding UI
        • Stage 2: Printing with ScriptX.Services
        • Stage 3: Summary and review
        • Stage 4: Error reporting
        • Stage 5: Prompted printing
        • Stage 6: Preparing for advanced uses
        • Stage 7: WaitForSpoolingComplete
        • Stage 8: Recommendations for some common issues
      • Printing with the API
      • MeadCoScriptXJS Library
      • Installing ScriptX.Services
        • For Windows PC
        • For On Premise Devices hosted on Windows Server
        • For On Premise Devices hosted on Windows 10/11
        • Configure options For On Premise Devices
        • Cloud
      • Orchestrator
      • Debugging
      • License deployment
        • For Windows PC
        • For On Premise Devices
      • Samples
        • Configure for Windows PC
        • Configure for On Premise
        • Configure for Cloud
    • Security Manager
      • Deploying a license or revision
    • ScriptX.Add-on
      • Introduction
      • Installing ScriptX on client PCs
      • Basic printing with ScriptX
      • Advanced printing features
      • Backwards compatibility
      • How to check if ScriptX is installed
      • License deployment
      • Quick start with Visual Studio
        • ASP.NET MVC
        • ASP.NET Web Forms
      • Nuget Packages
        • MeadCoScriptXJS Library
        • Installer helpers
        • ASP.NET WebForms Controls
        • Helpers for ASP.NET MVC
      • Client-side printing samples
  • Technical Reference
    • ScriptX.Services
      • Web service API
        • Service Description
          • (GET)
        • Licensing
          • licensing (GET)
          • licensing (POST)
          • licensing/ping (GET)
        • Printer
          • settings (GET)
          • current (GET)
          • current (PUT)
          • connection (PUT)
          • connection (DELETE)
        • PrintHtml
          • settings (GET)
          • deviceinfo (GET)
          • htmlPrintDefaults (GET)
          • print (POST)
          • status (GET)
          • download (GET)
          • canceljob (PUT)
        • PrintPdf
          • print (POST)
          • status (GET)
          • download (GET)
        • PrintDirect
          • print (POST)
      • Orchestrator API
        • v1
          • GET
        • v2
          • PUT
          • GET
      • ScriptX.Services compatibility roadmap
    • Security Manager
      • How it works
      • License Expiry
      • Testing for a valid license
      • About the license file (.mlf)
        • LICENSE
        • APPLICENSE
        • TITLE
        • DOMAINS
        • DOMAIN
        • PERMISSION
      • API
        • Apply
        • License
        • result
        • validLicense
    • ScriptX.Add-on
      • factory
        • baseUrl
        • ComponentVersionString
        • IsUniqueIDAvailable
        • OnDocumentComplete
        • relativeUrl
        • ResetUniqueID
        • ScriptXVersion
        • SecurityManagerVersion
        • Shutdown
        • UniqueID
      • printing
        • AddPrinterConnection
        • BatchPrintPDF
        • BatchPrintPDFEx
        • bottomMargin
        • collate
        • copies
        • currentPrinter
        • DefaultPrinter
        • disableUI
        • duplex
        • duplex2
        • EnumJobs
        • EnumPrinters
        • footer
        • GetJobsCount
        • GetMarginMeasure
        • header
        • headerFooterFont
        • IsSpooling
        • IsTemplateSupported
        • leftMargin
        • onafterprint
        • onbeforeprint
        • onbeforeunload
        • onpagesetup
        • onuserpagesetup
        • onuserprint
        • onuserprintpreview
        • orientation
        • OwnQueue
        • pageHeight
        • PageSetup
        • pageWidth
        • paperSize
        • paperSource
        • paperSource2
        • portrait
        • Preview
        • Print
        • printBackground
        • printer
        • PrintHTML
        • PrintHTMLEx
        • PrintPDF
        • PrintSetup
        • printToFileName
        • RemovePrinterConnection
        • rightMargin
        • SetMarginMeasure
        • SetPageRange
        • SetPreviewZoom
        • SetPrintScale
        • Sleep
        • templateURL
        • topMargin
        • TotalPrintPages
        • unprintableBottom
        • unprintableLeft
        • unprintableRight
        • unprintableTop
        • WaitForSpoolingComplete
      • printerControl
        • attributes
        • Bins
        • Forms
        • isLocal
        • isNetwork
        • isShared
        • Jobs
        • location
        • name
        • Pause
        • port
        • Purge
        • Resume
        • serverName
        • shareName
        • status
      • Job
        • Delete
        • Pause
        • Restart
        • Resume
      • enhancedFormatting
        • allFooterHeight
        • allHeaderHeight
        • allPagesFooter
        • allPagesHeader
        • extraFirstFooterHeight
        • extraFirstPageFooter
        • firstFooterHeight
        • firstHeaderHeight
        • firstPageFooter
        • firstPageHeader
        • pageRange
        • printingPass
      • rawPrinting
        • printer
        • printDocument
        • printString
    • Change and history logs
    • Articles
      • v1.15.x ScriptX Client Library
      • Dialogs with ScriptX.Services
      • Accessing protected content
      • Long term servicing (LTS)
 
ScriptX logotype
Home Getting started Documentation Samples Contact us

© 2025 Mead & Co Limited.

Follow us:
LinkedIn   GitHub
X

Warning:

This ScriptX.Add-on sample can only be viewed using Internet Explorer.