locked
Thread not completing after installation of Web API hosted on Windows service RRS feed

  • Question

  • User-201068877 posted

    Hi Everyone,

    I have one windows service that i want to print HTML files. So far in debug mode service is able to print the HTML document but when installed the same service print operation would not take place.

    We want to override the Print pop-up and send print directly to a default printer so that user need not to click on print button and no selection of printer required.

    In application flow is some thing like following.

    Web application call Print -> Call the Web API method (hosted using windows service)-> Print method write HTML to temp folder and Send print to default printer.

    Following method written for printing the page and running in windows service thread.

     public void PerformPrint(string url, string title = "untitled", int copies = 1) {
                if(string.IsNullOrEmpty(url))
                    throw new ArgumentException("The url can't be null or empty");

                Navigate(url);
                
                for (var i = 0; i < copies; i++) {
                    
                    //Whithout those lines your app will crash
                    const int enabledAndSupported = 
                        (int) OLECMDF.OLECMDF_SUPPORTED + (int) OLECMDF.OLECMDF_ENABLED;

                    while ((int)_webBrowser2.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != enabledAndSupported) {
                        Application.DoEvents();
                    }
                    //****************************************************************************************

                    _webBrowser2.ExecWB(
                        OLECMDID.OLECMDID_PRINT,
                        OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
                        null,
                        IntPtr.Zero);
                }
            }

    please help.

    Thanks & Regards,

    Naveen Upadhyay

    Tuesday, October 27, 2015 10:40 AM

All replies

  • User-219423983 posted

    Hi nav.upadhyay,

    I have one windows service that i want to print HTML files. So far in debug mode service is able to print the HTML document but when installed the same service print operation would not take place.

    You said the code could work in debug mode, dose it in the Windows Service? When you install the Service, you could check whether the service is running. If you want to print the directly, you could just use the WebBrowser.Print Method to achieve it. For more things, please take a look at the “Remarks” section in the following link.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.print%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    I hope it’s useful to you.

    Best Regard,

    Weibo Zhang

    Wednesday, October 28, 2015 6:11 AM