次の方法で共有


非同期出力のサンプル (POS for .NET v1.14 SDK ドキュメント)

Microsoft Point of Service for .NET (POS for .NET) は、Unified Point Of Service (UnifiedPOS) 仕様に準拠した非同期出力をサポートします。 非同期出力モデルでは、サービス オブジェクトはできるだけ早く制御をアプリケーションに返すことができるように、出力要求をキューに入れる必要があります。 要求が満たされると、2 番目のスレッドは OutputCompleteEvent イベントまたは ErrorEvent イベントを使用してデバイスに出力をディスパッチし、アプリケーションに通知する必要があります。

POS for .NET クラス ライブラリはサービス オブジェクト開発者向けにこれらの機能の大部分を処理するため、非同期出力デバイスと同期出力のみのデバイスの違いはほとんどありません。

プロジェクトを作成するには

  1. Visual Studio クラス ライブラリ プロジェクトを作成します。

  2. プロジェクトに以下のサンプル コードを追加します。

  3. Microsoft.PointOfService アセンブリへの参照を追加します。

  4. サービス オブジェクトをコンパイルして、サービス オブジェクト アセンブリの読み込みパスのいずれかのディレクトリにコピーします。

サービス オブジェクトでアプリケーション サンプルを使用するには

PosPrinter デバイスに出力するために、アプリケーションは通常 PrintNormal(PrinterStation, String)メソッドを使用します。 以下の PosPrinter サービス オブジェクト コードでは、このメソッドの実装が提供されないことに注意してください。 代わりに PrintNormalImpl(PrinterStation, PrinterState, String) が実装されます。 このメソッドは、同期と非同期の両方の出力要求に対して POS for .NET ライブラリによって呼び出されます。

アプリケーションが PrintNormal などの出力メソッドを呼び出すと、POS for .NET 実装環境が AsyncMode プロパティの値をチェックします。 この値が false の場合、POS for .NET ライブラリは要求をすぐに PrintNormalImpl に送信し、返されるのを待ちます。 ただし、値が true の場合、POS for .NET の PrintNormal 実装環境は内部管理キューに要求を追加します。 キューには項目がありますが、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
    }
}

イベント ハンドラーのサンプルのアプリケーション コードは、このサービス オブジェクトを使用することでコンパイルして実行できます。

参照

タスク

概念

その他の参照情報