Microsoft Point of Service for .NET (POS for .NET) 支持符合统一服务点 (UnifiedPOS) 规范的异步输出。 在异步输出模型中,服务对象必须将输出请求排入队列,以便它可以尽快将控制权返回给应用程序。 然后,第二个线程必须使用 OutputCompleteEvent 或 ErrorEvent 事件将输出分派给设备,并在满足请求时通知应用程序。
POS for .NET 类库为服务对象开发人员处理其中大部分函数,使异步输出设备与仅同步输出设备之间几乎没有差别。
创建项目
创建 Visual Studio 类库项目。
向项目添加示例代码。
添加对 Microsoft.PointOfService 程序集的引用。
编译服务对象并将其复制到服务对象程序集加载路径中的某个目录。
将应用程序示例与服务对象配合使用
- 此服务对象可与事件处理程序示例中提供的应用程序示例一起使用。
示例
为了输出到 PosPrinter 设备,应用程序最常使用 PrintNormal(PrinterStation, String) 方法。 请注意,下面的 PosPrinter 服务对象代码不提供此方法的实现。 而是实现 PrintNormalImpl(PrinterStation, PrinterState, String)。 对于同步和异步输出请求,此方法由 POS for .NET 库调用。
当应用程序调用输出方法(如 PrintNormal)时,POS for .NET 实现会检查 AsyncMode 属性的值。 如果此值为 false,POS for .NET 库会立即将请求发送到 PrintNormalImpl 并等待它返回。 但是,如果此值为 true,PrintNormal 的 POS for .NET 实现会将请求添加到内部管理的队列。 当队列中有项时,POS for .NET 托管线程将通过调用 PrintNormalImpl,按先入先出 (FIFO) 顺序将每个请求发送到设备。 当 PrintNormalImpl 返回时,库实现将在应用程序中引发 OutputCompleteEvent。 简言之,同样的服务对象代码可以同时支持同步和异步输出,而无需知道正在使用哪种输出模式。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Microsoft.PointOfService;
using Microsoft.PointOfService.BaseServiceObjects;
[assembly: PosAssembly("Service Object Contractors, Inc.")]
namespace SOSamples.AsyncOutput
{
[ServiceObject(
DeviceType.PosPrinter,
"AsyncOutputPrinter",
"Sample Async Printer",
1,
9)]
public class AsyncOutputSimulator : PosPrinterBase
{
public AsyncOutputSimulator()
{
DevicePath = "Sample Async Printer";
// Indicate that the Service Object supports
// the receipt printer.
Properties.CapRecPresent = true;
}
// Note that this method will be called by the POS for .NET
// library code, regardless of whether the print request
// is synchronous or asynchronous. The print request
// queue is managed completely by POS for .NET so the
// Service Object should simply write data to the device
// here.
protected override PrintResults PrintNormalImpl(
PrinterStation station,
PrinterState printerState,
string data)
{
// Your code to print to the actual hardware would go
// here.
// For demonstration, however, the code simulates
// that fulfilling this print request took 4 seconds.
Thread.Sleep(4000);
PrintResults results = new PrintResults();
return results;
}
// This method must be implemented by the Service
// Object, and should validate the data to be printed,
// including any escape sequences. This method should throw
// a PosControlException to indicate failure.
protected override void ValidateDataImpl(
PrinterStation station,
string data)
{
// Insert your validation code here.
return;
}
#region Implement Abstract PosCommon Members
private string MyHealthText = "";
// PosCommon.CheckHealthText.
public override string CheckHealthText
{
get
{
// VerifyState(mustBeClaimed,
// mustBeEnabled).
VerifyState(false, false);
return MyHealthText;
}
}
// PosCommon.CheckHealth.
public override string CheckHealth(
HealthCheckLevel level)
{
// Verify that device is open, claimed, and enabled.
VerifyState(true, true);
// Insert your code here:
// check the health of the device and return a
// descriptive string.
// Cache result in the CheckHealthText property.
MyHealthText = "Ok";
return MyHealthText;
}
// PosCommon.DirectIOData.
public override DirectIOData DirectIO(
int command,
int data,
object obj)
{
// Verify that the device is open.
VerifyState(false, false);
return new DirectIOData(data, obj);
}
#endregion Implement Abstract PosCommon Members
}
}
可以使用此服务对象编译并运行事件处理程序示例中的应用程序代码。