Example #1
0
 public HubManager(ServerConnectionSettings connectionSettings)
 {
     _connection = new HubConnectionBuilder()
                   .WithUrl($"{connectionSettings.ServerURL}/notification")
                   .WithAutomaticReconnect()
                   .Build();
     _connection.On <string>("botnewjobnotification", (agentId) => JobNotificationReceived?.Invoke(agentId));
 }
        /// <summary>
        /// Checks printer(s) for notifications.
        /// </summary>
        private void ListenForChanges()
        {
            // Check for cancellation.
            _cancelSource.Token.ThrowIfCancellationRequested();

            SafePrinterChangeNotificationHandle hChange = null;

            try
            {
                hChange = WaitForMultipleObjects(_hChanges);
            }
            catch (TimeoutException)
            {
                // No object changed within the timeout period.
                // This could mean that there was really no change,
                // or because the spooler is no longer sending updates to us.
                // Since we can't tell the difference, clean up existing structures and sign up for events again.

                // If the cancel token has been set, it might be a bit before we get back to checking it again,
                // so check it here before resetting.  If it has been set, bail out so the monitor can stop.
                _cancelSource.Token.ThrowIfCancellationRequested();

                LogDebug("No job changes detected.  Resetting listener.");
                Reset();
                Initialize();
                return;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                // Unsuccessful in getting the change notification handle.  Bail out.
                LogDebug(ex);
                return;
            }

            // WaitForMultipleObjects could have taken a while, so check for cancellation again.
            _cancelSource.Token.ThrowIfCancellationRequested();

            // Get the notify information that changed.
            PrinterNotifyInfoReader infoReader = FindNextPrinterChangeNotification(hChange);

            if (infoReader == null)
            {
                LogDebug("Lost job data.  Trying a refresh.");
                infoReader = FindNextPrinterChangeNotification(hChange, _options);
            }

            using (infoReader)
            {
                foreach (PrinterNotifyInfoData infoData in infoReader.ReadInfoData())
                {
                    if (infoData.NotifyType == NotifyType.Job)
                    {
                        JobNotificationReceived?.Invoke(this, new JobNotificationEventArgs(infoData));
                    }
                }
            }
        }