将 WebBrowser.Print() 变成阻塞函数

Turning WebBrowser.Print() into a blocking function

我正在使用 C# winforms 开发软件,并使用 WebBrowser 控件。我已经意识到 Print() 函数是非阻塞的,但我需要在打印过程之前和之后执行一个函数。有什么方法可以检查打印事件或任何其他可以让我停止程序执行直到打印完成的属性吗?

打印功能应该只向计算机的打印队列添加一个作业,而且速度很快。但是使用 System.Printing.PrintServer 和 System.Printing.PrintQueue 类 你也许可以。

  1. 使用 new PrintServer() 为您的本地计算机获取一个实例。
  2. 然后在实例上使用 GetPrintQueues() 获取所有本地队列。
  3. 然后遍历队列并检查它们的 NumberOfJobs 属性 以检查是否还有任何项目需要打印。

如果您想要一个活动,您实际上必须自己实施该活动。喜欢:

public static event Action OnEmptyPrintQueue;
private static void Main(string[] args)
{
    new Task(() =>
    {
        var wasEmpty = true;
        while (true)
        {
            if (wasEmpty && NumberOfJobs > 0)
            {
                wasEmpty = false;
            }
            else if (!wasEmpty && NumberOfJobs == 0)
            {
                wasEmpty = true;
                OnEmptyPrintQueue?.Invoke();
            }
        }

    }, TaskCreationOptions.LongRunning).Start();
}