Ejemplo n.º 1
0
        /// <summary>
        /// Connects to a remote print queue at the specified path.
        /// </summary>
        /// <param name="printerPath">The printer path.</param>
        /// <returns><c>true</c> if the remote queue connection was successful, <c>false</c> otherwise.</returns>
        public static bool ConnectToRemoteQueue(string printerPath)
        {
            LogDebug($"Adding connection to remote queue '{printerPath}'.");

            // In busy situations it may take a few attempts to connect to the print server.
            // Retry a few times with a delay in between.
            bool connect()
            {
                try
                {
                    using (LocalPrintServer server = new LocalPrintServer())
                    {
                        return(server.ConnectToPrintQueue(printerPath));
                    }
                }
                catch (SystemException ex) when(ex.Message.Contains("Win32"))
                {
                    LogWarn($"Remote queue connection failure: {ex.Message}");
                    return(false);
                }
            }

            bool success = Retry.UntilTrue(connect, 10, TimeSpan.FromSeconds(5));

            if (success)
            {
                LogInfo($"Successfully connected to remote queue '{printerPath}'.");
            }
            else
            {
                LogWarn($"Unable to connect to remote queue '{printerPath}'.");
            }
            return(success);
        }