WaitForDrawerClose 阻止打印

WaitForDrawerClose blocks printing

我正在编写一个使用 Pos for .Net 的应用程序,我注意到如果您调用 WaitForDrawerClose,那么在收据返回之前您将无法打印收据。

这是不可取的行为。有没有其他方法可以在不阻塞打印机的情况下等待现金抽屉关闭?

我查看了 OnDrawerStateChanged Event,但它是 CashDrawerBase 的受保护成员,我不确定如何访问它。

这是我的 SSCCE:

static void Main(string[] args)
{
    var posExplorer = new PosExplorer();
    var waitTask = WaitForCloseAsync(posExplorer);
    System.Threading.Thread.Sleep(500);
    PrintText(posExplorer);
    waitTask.Wait();
}

public static Task WaitForCloseAsync(PosExplorer posExplorer)
{
    var result = Task.Factory.StartNew(() =>
    {
        Console.WriteLine("waiting");
        var cashDrawer = GetCashDrawer(posExplorer);
        cashDrawer.Open();
        cashDrawer.Claim(1000);
        cashDrawer.DeviceEnabled = true;
        cashDrawer.WaitForDrawerClose(10000, 4000, 500, 5000);
        cashDrawer.Release();
        cashDrawer.Close();
        Console.WriteLine("waited");
    });
    return result;
}

public static void PrintText(PosExplorer posExplorer)
{
    Console.WriteLine("printing");
    var printer = GetPosPrinter(posExplorer);
    printer.Open();
    printer.Claim(1000);
    printer.DeviceEnabled = true;
    var text = "abc\x1B|1lF";
    printer.PrintNormal(PrinterStation.Receipt, text);
    printer.Release();
    printer.Close();
    Console.WriteLine("printed");
}

public static CashDrawer GetCashDrawer(PosExplorer posExplorer)
{
    var deviceInfo = posExplorer.GetDevices(DeviceCompatibilities.Opos)
        .Cast<DeviceInfo>()
        .Where(d => d.Type == "CashDrawer")
        .ToList();

    var device = deviceInfo.FirstOrDefault(d => d.Compatibility == DeviceCompatibilities.Opos);

    if (device == null)
    {
        return null;
    }
    else
        return (CashDrawer)posExplorer.CreateInstance(device);
}

private static PosPrinter GetPosPrinter(PosExplorer posExplorer)
{
    var deviceInfo = posExplorer.GetDevices(DeviceCompatibilities.Opos)
        .Cast<DeviceInfo>()
        .Where(d => d.Type == "PosPrinter")
        .ToList();

    var device = deviceInfo.FirstOrDefault(d => d.Compatibility == DeviceCompatibilities.Opos);

    if (device == null)
    {
        return null;
    }
    else
    {
        return (PosPrinter)posExplorer.CreateInstance(device);
    }
}

所以,我所做的基本上是这样的:我没有使用 (WaitForClose),而是像这样轮询它:

        for (var i = 0; i < 15; i++)
        {
            cashDrawer = GetCashDrawer(posExplorer);
            cashDrawer.Open();
            cashDrawer.Claim(1000);
            cashDrawer.DeviceEnabled = true;
            if (!cashDrawer.DrawerOpened)
            {
                Console.WriteLine("waited");
                return;
            }                    
            cashDrawer.Release();
            cashDrawer.Close();
            System.Threading.Thread.Sleep(1500);
        }
        Console.WriteLine("timed out");

这并不理想,但它也不会锁定打印机,所以现在必须锁定。