﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
// Counter header
using CcntCs;

namespace CounterCallback
{
    /// <summary>
    /// Summary description for CounterCallback.
    /// </summary>
    public class CounterCallback : System.Windows.Forms.Form
    {
        public System.Windows.Forms.GroupBox Frame2;
        public System.Windows.Forms.Label lblNotify;
        public System.Windows.Forms.Button btnCntExit;
        public System.Windows.Forms.Button btnCntNotify;
        public System.Windows.Forms.Button btnCntMode;
        public System.Windows.Forms.Button btnCntInit;
        public System.Windows.Forms.Label Label6;
        public System.Windows.Forms.Button btnCntStop;
        public System.Windows.Forms.Button btnCntPreset;
        public System.Windows.Forms.Button btnCntStart;
        public System.Windows.Forms.TextBox txtRet;
        public System.Windows.Forms.GroupBox Frame1;
        public System.Windows.Forms.TextBox txtCountStatus0;
        public System.Windows.Forms.TextBox txtCountStatus3;
        public System.Windows.Forms.TextBox txtCountStatus2;
        public System.Windows.Forms.TextBox txtCountStatus1;
        public System.Windows.Forms.TextBox txtCountValue0;
        public System.Windows.Forms.TextBox txtCountValue3;
        public System.Windows.Forms.TextBox txtCountValue2;
        public System.Windows.Forms.TextBox txtCountValue1;
        public System.Windows.Forms.Label Label2;
        public System.Windows.Forms.Label Label5;
        public System.Windows.Forms.Label Label4;
        public System.Windows.Forms.Label Label3;
        public System.Windows.Forms.Button btnCntReset;
        public System.Windows.Forms.TextBox txtDeviceName;
        public System.Windows.Forms.Label Label1;
        public System.Windows.Forms.Button btnExit;
        public System.Windows.Forms.Button btnCntEvent;
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.Timer timer1;

        public static short                     Id;                                  // Device ID
        public static short                     ChannelNum;                          // Total number of channels
        uint[]                                  CountMatch = new uint[CHANNEL_NUM];  // Count match times
        public const int                        CHANNEL_NUM = 4;
        const int                               WM_NOTIFY_COUNTUP = 0x0500;
        const int                               WM_NOTIFY_ERROR = 0x0501;
        const int                               WM_NOTIFY_CARRY = 0x0502;
        private System.Windows.Forms.TextBox[]  txt_Count = new System.Windows.Forms.TextBox[CHANNEL_NUM];    // Count value control
        private System.Windows.Forms.TextBox[]  txt_Status = new System.Windows.Forms.TextBox[CHANNEL_NUM];   // Count status control

        // Declare callback routine
        static public    GCHandle                hGC_CountUp;
        static public    GCHandle                hGC_CounterError;
        static public    GCHandle                hGC_CarryBorrow;
        static public    GCHandle                hGC_Timer;
        static public    PCOUNTUPCALLBACK        dele_CountUp;
        static public    PCOUNTERERRORCALLBACK    dele_CounterError;
        static public    PCARRYBORROWCALLBACK    dele_CarryBorrow;
        //---------------------------------------------
        // When using the timer notification event of device,
        // please use the following declaration
        //---------------------------------------------
        //static public    PTIMERCALLBACK            dele_Timer;
        static public    IntPtr                    pCountUp;
        static public    IntPtr                    pCounterError;
        static public    IntPtr                    pCarryBorrow;
        static public    IntPtr                    pTimer;

        Ccnt             cnt = new Ccnt();


        public CounterCallback()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //-----------------------------------
            // Control array settings
            //-----------------------------------
            //------------------
            // Count value control
            //------------------
            txt_Count[0] = txtCountValue0;
            txt_Count[1] = txtCountValue1;
            txt_Count[2] = txtCountValue2;
            txt_Count[3] = txtCountValue3;
            //------------------
            // Count status control
            //------------------
            txt_Status[0] = txtCountStatus0;
            txt_Status[1] = txtCountStatus1;
            txt_Status[2] = txtCountStatus2;
            txt_Status[3] = txtCountStatus3;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.Frame2 = new System.Windows.Forms.GroupBox();
            this.lblNotify = new System.Windows.Forms.Label();
            this.btnCntExit = new System.Windows.Forms.Button();
            this.btnCntNotify = new System.Windows.Forms.Button();
            this.btnCntMode = new System.Windows.Forms.Button();
            this.btnCntInit = new System.Windows.Forms.Button();
            this.Label6 = new System.Windows.Forms.Label();
            this.btnCntStop = new System.Windows.Forms.Button();
            this.btnCntPreset = new System.Windows.Forms.Button();
            this.btnCntStart = new System.Windows.Forms.Button();
            this.txtRet = new System.Windows.Forms.TextBox();
            this.Frame1 = new System.Windows.Forms.GroupBox();
            this.txtCountStatus0 = new System.Windows.Forms.TextBox();
            this.txtCountStatus3 = new System.Windows.Forms.TextBox();
            this.txtCountStatus2 = new System.Windows.Forms.TextBox();
            this.txtCountStatus1 = new System.Windows.Forms.TextBox();
            this.txtCountValue0 = new System.Windows.Forms.TextBox();
            this.txtCountValue3 = new System.Windows.Forms.TextBox();
            this.txtCountValue2 = new System.Windows.Forms.TextBox();
            this.txtCountValue1 = new System.Windows.Forms.TextBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label5 = new System.Windows.Forms.Label();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label3 = new System.Windows.Forms.Label();
            this.btnCntReset = new System.Windows.Forms.Button();
            this.txtDeviceName = new System.Windows.Forms.TextBox();
            this.Label1 = new System.Windows.Forms.Label();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnCntEvent = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.Frame2.SuspendLayout();
            this.Frame1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Frame2
            // 
            this.Frame2.BackColor = System.Drawing.SystemColors.Control;
            this.Frame2.Controls.Add(this.lblNotify);
            this.Frame2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame2.Location = new System.Drawing.Point(8, 200);
            this.Frame2.Name = "Frame2";
            this.Frame2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame2.Size = new System.Drawing.Size(296, 112);
            this.Frame2.TabIndex = 52;
            this.Frame2.TabStop = false;
            this.Frame2.Text = "Notification:";
            // 
            // lblNotify
            // 
            this.lblNotify.BackColor = System.Drawing.SystemColors.Control;
            this.lblNotify.Cursor = System.Windows.Forms.Cursors.Default;
            this.lblNotify.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblNotify.Location = new System.Drawing.Point(8, 28);
            this.lblNotify.Name = "lblNotify";
            this.lblNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblNotify.Size = new System.Drawing.Size(272, 68);
            this.lblNotify.TabIndex = 20;
            this.lblNotify.Text = "Notification:";
            // 
            // btnCntExit
            // 
            this.btnCntExit.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntExit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntExit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntExit.Location = new System.Drawing.Point(320, 288);
            this.btnCntExit.Name = "btnCntExit";
            this.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntExit.Size = new System.Drawing.Size(105, 28);
            this.btnCntExit.TabIndex = 46;
            this.btnCntExit.Text = "CntExit";
            this.btnCntExit.UseVisualStyleBackColor = false;
            this.btnCntExit.Click += new System.EventHandler(this.btnCntExit_Click);
            // 
            // btnCntNotify
            // 
            this.btnCntNotify.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntNotify.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntNotify.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntNotify.Location = new System.Drawing.Point(320, 78);
            this.btnCntNotify.Name = "btnCntNotify";
            this.btnCntNotify.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntNotify.Size = new System.Drawing.Size(105, 28);
            this.btnCntNotify.TabIndex = 40;
            this.btnCntNotify.Text = "Notification...";
            this.btnCntNotify.UseVisualStyleBackColor = false;
            this.btnCntNotify.Click += new System.EventHandler(this.btnCntNotify_Click);
            // 
            // btnCntMode
            // 
            this.btnCntMode.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntMode.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntMode.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntMode.Location = new System.Drawing.Point(320, 43);
            this.btnCntMode.Name = "btnCntMode";
            this.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntMode.Size = new System.Drawing.Size(105, 28);
            this.btnCntMode.TabIndex = 39;
            this.btnCntMode.Text = "Counter Mode...";
            this.btnCntMode.UseVisualStyleBackColor = false;
            this.btnCntMode.Click += new System.EventHandler(this.btnCntMode_Click);
            // 
            // btnCntInit
            // 
            this.btnCntInit.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntInit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntInit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntInit.Location = new System.Drawing.Point(320, 8);
            this.btnCntInit.Name = "btnCntInit";
            this.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntInit.Size = new System.Drawing.Size(105, 28);
            this.btnCntInit.TabIndex = 38;
            this.btnCntInit.Text = "CntInit";
            this.btnCntInit.UseVisualStyleBackColor = false;
            this.btnCntInit.Click += new System.EventHandler(this.btnCntInit_Click);
            // 
            // Label6
            // 
            this.Label6.AutoSize = true;
            this.Label6.BackColor = System.Drawing.SystemColors.Control;
            this.Label6.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label6.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label6.Location = new System.Drawing.Point(8, 331);
            this.Label6.Name = "Label6";
            this.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label6.Size = new System.Drawing.Size(72, 13);
            this.Label6.TabIndex = 53;
            this.Label6.Text = "Return Value:";
            // 
            // btnCntStop
            // 
            this.btnCntStop.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntStop.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntStop.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntStop.Location = new System.Drawing.Point(320, 253);
            this.btnCntStop.Name = "btnCntStop";
            this.btnCntStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStop.Size = new System.Drawing.Size(105, 28);
            this.btnCntStop.TabIndex = 45;
            this.btnCntStop.Text = "CntStop";
            this.btnCntStop.UseVisualStyleBackColor = false;
            this.btnCntStop.Click += new System.EventHandler(this.btnCntStop_Click);
            // 
            // btnCntPreset
            // 
            this.btnCntPreset.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntPreset.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntPreset.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntPreset.Location = new System.Drawing.Point(320, 183);
            this.btnCntPreset.Name = "btnCntPreset";
            this.btnCntPreset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntPreset.Size = new System.Drawing.Size(105, 28);
            this.btnCntPreset.TabIndex = 43;
            this.btnCntPreset.Text = "CntPreset";
            this.btnCntPreset.UseVisualStyleBackColor = false;
            this.btnCntPreset.Click += new System.EventHandler(this.btnCntPreset_Click);
            // 
            // btnCntStart
            // 
            this.btnCntStart.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntStart.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntStart.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntStart.Location = new System.Drawing.Point(320, 148);
            this.btnCntStart.Name = "btnCntStart";
            this.btnCntStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntStart.Size = new System.Drawing.Size(105, 28);
            this.btnCntStart.TabIndex = 42;
            this.btnCntStart.Text = "CntStart";
            this.btnCntStart.UseVisualStyleBackColor = false;
            this.btnCntStart.Click += new System.EventHandler(this.btnCntStart_Click);
            // 
            // txtRet
            // 
            this.txtRet.AcceptsReturn = true;
            this.txtRet.BackColor = System.Drawing.SystemColors.Window;
            this.txtRet.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtRet.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtRet.Location = new System.Drawing.Point(88, 327);
            this.txtRet.MaxLength = 0;
            this.txtRet.Name = "txtRet";
            this.txtRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtRet.Size = new System.Drawing.Size(217, 20);
            this.txtRet.TabIndex = 50;
            // 
            // Frame1
            // 
            this.Frame1.BackColor = System.Drawing.SystemColors.Control;
            this.Frame1.Controls.Add(this.txtCountStatus0);
            this.Frame1.Controls.Add(this.txtCountStatus3);
            this.Frame1.Controls.Add(this.txtCountStatus2);
            this.Frame1.Controls.Add(this.txtCountStatus1);
            this.Frame1.Controls.Add(this.txtCountValue0);
            this.Frame1.Controls.Add(this.txtCountValue3);
            this.Frame1.Controls.Add(this.txtCountValue2);
            this.Frame1.Controls.Add(this.txtCountValue1);
            this.Frame1.Controls.Add(this.Label2);
            this.Frame1.Controls.Add(this.Label5);
            this.Frame1.Controls.Add(this.Label4);
            this.Frame1.Controls.Add(this.Label3);
            this.Frame1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame1.Location = new System.Drawing.Point(8, 40);
            this.Frame1.Name = "Frame1";
            this.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame1.Size = new System.Drawing.Size(296, 150);
            this.Frame1.TabIndex = 49;
            this.Frame1.TabStop = false;
            this.Frame1.Text = "Count Value:";
            // 
            // txtCountStatus0
            // 
            this.txtCountStatus0.AcceptsReturn = true;
            this.txtCountStatus0.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus0.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus0.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus0.Location = new System.Drawing.Point(179, 22);
            this.txtCountStatus0.MaxLength = 0;
            this.txtCountStatus0.Name = "txtCountStatus0";
            this.txtCountStatus0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus0.Size = new System.Drawing.Size(71, 20);
            this.txtCountStatus0.TabIndex = 24;
            // 
            // txtCountStatus3
            // 
            this.txtCountStatus3.AcceptsReturn = true;
            this.txtCountStatus3.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus3.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus3.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus3.Location = new System.Drawing.Point(179, 109);
            this.txtCountStatus3.MaxLength = 0;
            this.txtCountStatus3.Name = "txtCountStatus3";
            this.txtCountStatus3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus3.Size = new System.Drawing.Size(71, 20);
            this.txtCountStatus3.TabIndex = 27;
            // 
            // txtCountStatus2
            // 
            this.txtCountStatus2.AcceptsReturn = true;
            this.txtCountStatus2.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus2.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus2.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus2.Location = new System.Drawing.Point(179, 80);
            this.txtCountStatus2.MaxLength = 0;
            this.txtCountStatus2.Name = "txtCountStatus2";
            this.txtCountStatus2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus2.Size = new System.Drawing.Size(71, 20);
            this.txtCountStatus2.TabIndex = 26;
            // 
            // txtCountStatus1
            // 
            this.txtCountStatus1.AcceptsReturn = true;
            this.txtCountStatus1.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountStatus1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountStatus1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountStatus1.Location = new System.Drawing.Point(179, 51);
            this.txtCountStatus1.MaxLength = 0;
            this.txtCountStatus1.Name = "txtCountStatus1";
            this.txtCountStatus1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountStatus1.Size = new System.Drawing.Size(71, 20);
            this.txtCountStatus1.TabIndex = 25;
            // 
            // txtCountValue0
            // 
            this.txtCountValue0.AcceptsReturn = true;
            this.txtCountValue0.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue0.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue0.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue0.Location = new System.Drawing.Point(80, 22);
            this.txtCountValue0.MaxLength = 0;
            this.txtCountValue0.Name = "txtCountValue0";
            this.txtCountValue0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue0.Size = new System.Drawing.Size(71, 20);
            this.txtCountValue0.TabIndex = 12;
            // 
            // txtCountValue3
            // 
            this.txtCountValue3.AcceptsReturn = true;
            this.txtCountValue3.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue3.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue3.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue3.Location = new System.Drawing.Point(80, 109);
            this.txtCountValue3.MaxLength = 0;
            this.txtCountValue3.Name = "txtCountValue3";
            this.txtCountValue3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue3.Size = new System.Drawing.Size(71, 20);
            this.txtCountValue3.TabIndex = 15;
            // 
            // txtCountValue2
            // 
            this.txtCountValue2.AcceptsReturn = true;
            this.txtCountValue2.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue2.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue2.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue2.Location = new System.Drawing.Point(80, 80);
            this.txtCountValue2.MaxLength = 0;
            this.txtCountValue2.Name = "txtCountValue2";
            this.txtCountValue2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue2.Size = new System.Drawing.Size(71, 20);
            this.txtCountValue2.TabIndex = 14;
            // 
            // txtCountValue1
            // 
            this.txtCountValue1.AcceptsReturn = true;
            this.txtCountValue1.BackColor = System.Drawing.SystemColors.Window;
            this.txtCountValue1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtCountValue1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtCountValue1.Location = new System.Drawing.Point(80, 51);
            this.txtCountValue1.MaxLength = 0;
            this.txtCountValue1.Name = "txtCountValue1";
            this.txtCountValue1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtCountValue1.Size = new System.Drawing.Size(71, 20);
            this.txtCountValue1.TabIndex = 13;
            // 
            // Label2
            // 
            this.Label2.AutoSize = true;
            this.Label2.BackColor = System.Drawing.SystemColors.Control;
            this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label2.Location = new System.Drawing.Point(8, 28);
            this.Label2.Name = "Label2";
            this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label2.Size = new System.Drawing.Size(31, 13);
            this.Label2.TabIndex = 23;
            this.Label2.Text = "CH0:";
            // 
            // Label5
            // 
            this.Label5.AutoSize = true;
            this.Label5.BackColor = System.Drawing.SystemColors.Control;
            this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label5.Location = new System.Drawing.Point(8, 115);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(31, 13);
            this.Label5.TabIndex = 15;
            this.Label5.Text = "CH3:";
            // 
            // Label4
            // 
            this.Label4.AutoSize = true;
            this.Label4.BackColor = System.Drawing.SystemColors.Control;
            this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label4.Location = new System.Drawing.Point(8, 86);
            this.Label4.Name = "Label4";
            this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label4.Size = new System.Drawing.Size(31, 13);
            this.Label4.TabIndex = 14;
            this.Label4.Text = "CH2:";
            // 
            // Label3
            // 
            this.Label3.AutoSize = true;
            this.Label3.BackColor = System.Drawing.SystemColors.Control;
            this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label3.Location = new System.Drawing.Point(8, 57);
            this.Label3.Name = "Label3";
            this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label3.Size = new System.Drawing.Size(31, 13);
            this.Label3.TabIndex = 13;
            this.Label3.Text = "CH1:";
            // 
            // btnCntReset
            // 
            this.btnCntReset.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntReset.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntReset.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntReset.Location = new System.Drawing.Point(320, 218);
            this.btnCntReset.Name = "btnCntReset";
            this.btnCntReset.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntReset.Size = new System.Drawing.Size(105, 28);
            this.btnCntReset.TabIndex = 44;
            this.btnCntReset.Text = "CntReset";
            this.btnCntReset.UseVisualStyleBackColor = false;
            this.btnCntReset.Click += new System.EventHandler(this.btnCntReset_Click);
            // 
            // txtDeviceName
            // 
            this.txtDeviceName.AcceptsReturn = true;
            this.txtDeviceName.BackColor = System.Drawing.SystemColors.Window;
            this.txtDeviceName.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.txtDeviceName.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtDeviceName.Location = new System.Drawing.Point(88, 12);
            this.txtDeviceName.MaxLength = 0;
            this.txtDeviceName.Name = "txtDeviceName";
            this.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtDeviceName.Size = new System.Drawing.Size(71, 20);
            this.txtDeviceName.TabIndex = 48;
            // 
            // Label1
            // 
            this.Label1.AutoSize = true;
            this.Label1.BackColor = System.Drawing.SystemColors.Control;
            this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label1.Location = new System.Drawing.Point(8, 16);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(75, 13);
            this.Label1.TabIndex = 51;
            this.Label1.Text = "Device Name:";
            // 
            // btnExit
            // 
            this.btnExit.BackColor = System.Drawing.SystemColors.Control;
            this.btnExit.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnExit.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnExit.Location = new System.Drawing.Point(320, 323);
            this.btnExit.Name = "btnExit";
            this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnExit.Size = new System.Drawing.Size(105, 28);
            this.btnExit.TabIndex = 47;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = false;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // btnCntEvent
            // 
            this.btnCntEvent.BackColor = System.Drawing.SystemColors.Control;
            this.btnCntEvent.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCntEvent.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnCntEvent.Location = new System.Drawing.Point(320, 113);
            this.btnCntEvent.Name = "btnCntEvent";
            this.btnCntEvent.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btnCntEvent.Size = new System.Drawing.Size(105, 28);
            this.btnCntEvent.TabIndex = 41;
            this.btnCntEvent.Text = "Event...";
            this.btnCntEvent.UseVisualStyleBackColor = false;
            this.btnCntEvent.Click += new System.EventHandler(this.btnCntEvent_Click);
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // CounterCallback
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(437, 358);
            this.Controls.Add(this.Frame2);
            this.Controls.Add(this.btnCntExit);
            this.Controls.Add(this.btnCntNotify);
            this.Controls.Add(this.btnCntMode);
            this.Controls.Add(this.btnCntInit);
            this.Controls.Add(this.Label6);
            this.Controls.Add(this.btnCntStop);
            this.Controls.Add(this.btnCntPreset);
            this.Controls.Add(this.btnCntStart);
            this.Controls.Add(this.txtRet);
            this.Controls.Add(this.Frame1);
            this.Controls.Add(this.btnCntReset);
            this.Controls.Add(this.txtDeviceName);
            this.Controls.Add(this.Label1);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnCntEvent);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "CounterCallback";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Counter";
            this.Load += new System.EventHandler(this.CounterCallback_Load);
            this.Frame2.ResumeLayout(false);
            this.Frame1.ResumeLayout(false);
            this.Frame1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new CounterCallback());
        }

        //===================================================
        // Dialog initialization
        //===================================================
        unsafe private void CounterCallback_Load(object sender, System.EventArgs e)
        {
            short   ch;

            //-----------------------------------
            // Initialize dialog display
            //-----------------------------------
            txtDeviceName.Text = "CNT000";
            //-----------------------------------
            // For each channel
            //-----------------------------------
            for(ch = 0; ch < CHANNEL_NUM; ch++){
                //------------------
                // Count value control
                //------------------
                txt_Count[ch].Text = "";
                //------------------
                // Count status control
                //------------------
                txt_Status[ch].Text = "";
            }
            //-----------------------------------
            // Initialize the delegate for event notification
            //-----------------------------------
            dele_CountUp    = new PCOUNTUPCALLBACK(SetCountUpCallBackProc);
            dele_CounterError    = new PCOUNTERERRORCALLBACK(CounterErrorCallBackProc);
            dele_CarryBorrow    = new PCARRYBORROWCALLBACK(CarryBorrowCallBackProc);
            //-----------------------------------
            // Do not release the delegate
            //-----------------------------------
            if(hGC_CountUp.IsAllocated == false)
            {
                hGC_CountUp    = GCHandle.Alloc(dele_CountUp);
            }
            if(hGC_CounterError.IsAllocated == false)
            {
                hGC_CounterError    = GCHandle.Alloc(dele_CounterError);
            }
            if(hGC_CarryBorrow.IsAllocated == false)
            {
                hGC_CarryBorrow    = GCHandle.Alloc(dele_CarryBorrow);
            }
            /*
            //---------------------------------------------
            // When using the timer notification event of device,
            // please use the following settings
            //---------------------------------------------
            //---------------------------------------------
            // Initialize the delegate for timer event notification
            //---------------------------------------------
            dele_Timer = new PTIMERCALLBACK(TimerCallBackProc);
            //---------------------------------------------
            // Set the delegate not to release
            //---------------------------------------------
            if(hGC_Timer.IsAllocated == false)
            {
                hGC_Timer    = GCHandle.Alloc(dele_Timer);
            }
            */
        }

        //---------------------------------------------
        // CntInit()
        //---------------------------------------------
        private void btnCntInit_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    buf = new string(' ', 100);
            string    error_string = new string(' ', 256);
            int       ret;
            short     i;
    
            //-----------------------------------
            // Get device name
            //-----------------------------------
            buf = txtDeviceName.Text;

            //------------------------------
            // Initialization
            //------------------------------
            ret = cnt.Init(buf, out Id);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntInit ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
            
            if(ret != 0){
                return;
            }
            //-----------------------------
            // Get number of channels
            //-----------------------------
            cnt.GetMaxChannels(Id, out ChannelNum);
            //-----------------------------
            // If the sample control count is exceeded, 
            // the total channel count is corrected to the control count.
            //-----------------------------
            if(ChannelNum > CHANNEL_NUM){
                ChannelNum = CHANNEL_NUM;
            }
            //-----------------------------
            // Toggles the count value and status display according to the total number of channels.
            //-----------------------------
            for(i = 0; i < CHANNEL_NUM; i++){
                //-----------------------------
                // Disables the display when the number of loops exceeds the total number of channels.
                //-----------------------------
                if(i >= ChannelNum){
                    txt_Count[i].Enabled = false;
                    txt_Status[i].Enabled = false;
                //-----------------------------
                // Enables the display when the number of loops is less than or equal to the total number of channels.
                //-----------------------------
                }else{
                    txt_Count[i].Enabled = true;
                    txt_Status[i].Enabled = true;
                }
            }
        }

        //---------------------------------------------
        // Display "Counter Mode Setting" dialog
        //---------------------------------------------
        private void btnCntMode_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display "Counter Mode Setting" dialog
            //------------------------------
            ModeDlg    dlg = new ModeDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // Display "Notification Setting" dialog
        //---------------------------------------------
        private void btnCntNotify_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display "Notification Setting" dialog
            //------------------------------
            NotifyDlg dlg = new NotifyDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // Display "Event Setting" dialog
        //---------------------------------------------
        private void btnCntEvent_Click(object sender, System.EventArgs e)
        {
            //------------------------------
            // Display "Event Setting" dialog
            //------------------------------
            EventDlg dlg = new EventDlg();
            dlg.ShowDialog();
            dlg.Dispose();
        }

        //---------------------------------------------
        // Start count
        //---------------------------------------------
        unsafe private void btnCntStart_Click(object sender, System.EventArgs e)
        {
            string  str;
            string  error_string = new string(' ', 256);
            int     ret;
            short[] ChNo = new short[CHANNEL_NUM];
            short   i;

            //-----------------------------
            // Callback rountine
            //-----------------------------
            // Count match
            pCountUp = Marshal.GetFunctionPointerForDelegate(dele_CountUp);
            ret = cnt.CountUpCallbackProc(Id, pCountUp, null);
            /*
            //-----------------------------
            // The error check is commented out for devices that do not have the event notification function
            // If you need the error check, uncomment it
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntSetCountUpCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */
            // Counter error
            pCounterError = Marshal.GetFunctionPointerForDelegate(dele_CounterError);
            ret = cnt.CounterErrorCallbackProc(Id, pCounterError, null);
            /*
            //-----------------------------
            // The error check is commented out for devices that do not have the event notification function
            // If you need the error check, uncomment it
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntSetCounterErrorCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */
            // Carry/Borrow
            pCarryBorrow = Marshal.GetFunctionPointerForDelegate(dele_CarryBorrow);
            ret = cnt.CarryBorrowCallbackProc(Id, pCarryBorrow, null);
            /*
            //-----------------------------
            // The error check is commented out for devices that do not have the event notification function
            // If you need the error check, uncomment it
            //-----------------------------
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntCarryBorrowCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */
            /*
            //---------------------------------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //---------------------------------------------------------
            // Timer
            pTimer = Marshal.GetFunctionPointerForDelegate(dele_Timer);
            // Timer notification
            ret = cnt.TimerCallbackProc(Id, pTimer, null);
            if(ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntTimerCallbackProc ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                return;
            }
            */

            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Start count
            //------------------------------
            ret = cnt.StartCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntStartCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
            if (ret == 0) 
            {
                //------------------------------
                // Consider a device that does not have a timer notification function,
                // we use the window timer.
                // Please switch and use, according to the use.
                //------------------------------
                //------------------------------
                // Start timer
                //------------------------------
                timer1.Start();
                //------------------------------
                // When using the timer notification event of device,
                // please use the following function.
                //------------------------------
                //cnt.NotifyTimer(Id, 100, this.Handle);
            }
        }

        //---------------------------------------------
        // Preset counter
        //---------------------------------------------
        private void btnCntPreset_Click(object sender, System.EventArgs e)
        {
            string  str;
            string  error_string = new string(' ', 256);
            int     ret;
            short[] ChNo = new short[CHANNEL_NUM];
            uint[]  PresetData = new uint[CHANNEL_NUM];
            short   i;

            //------------------------------
            // Set preset value
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
                PresetData[i] = (uint)i * 0x100;
            }

            //------------------------------
            // Preset setting processing
            //------------------------------
            ret = cnt.Preset(Id, ChNo, ChannelNum, PresetData);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntPreset ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // Reset counter
        //---------------------------------------------
        private void btnCntReset_Click(object sender, System.EventArgs e)
        {
            string  str;
            string  error_string = new string(' ', 256);
            int     ret;
            short[] ChNo = new short[CHANNEL_NUM];
            short   i;

            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Reset counter
            //------------------------------
            ret = cnt.ZeroClearCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntZeroClearCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // Stop count
        //---------------------------------------------
        private void btnCntStop_Click(object sender, System.EventArgs e)
        {
            string  str;
            string  error_string = new string(' ', 256);
            int     ret;
            short[] ChNo = new short[CHANNEL_NUM];
            short   i;

            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
            }

            //------------------------------
            // Stop count
            //------------------------------
            ret = cnt.StopCount(Id, ChNo, ChannelNum);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntStopCount ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);
        }

        //---------------------------------------------
        // CntExit()
        //---------------------------------------------
        private void btnCntExit_Click(object sender, System.EventArgs e)
        {
            string    str;
            string    error_string = new string(' ', 256);
            int       ret;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // Counter termination processing
            //------------------------------
            ret = cnt.Exit(Id);
            cnt.GetErrorString(ret, out error_string);
            str = string.Format("CntExit ret = {0:d}: {1}", ret, error_string);
            txtRet.Text = str;
        }

        //---------------------------------------------
        // Exit sample program
        //---------------------------------------------
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            int    ret;

            //------------------------------
            // Consider a device that does not have a timer notification function,
            // we use the window timer.
            // Please switch and use, according to the use.
            //------------------------------
            //------------------------------
            // Stop timer
            //------------------------------
            timer1.Stop();
            //------------------------------
            // When using the timer notification event of device,
            // please use the following function.
            //------------------------------
            //cnt.StopNotifyTimer(Id);

            //------------------------------
            // Counter termination processing
            //------------------------------
            ret = cnt.Exit(Id);
            //-----------------------------
            // Release the garbage collection handle of the delegate for event notification
            //-----------------------------
            if(hGC_CountUp.IsAllocated == true)
            {
                hGC_CountUp.Free();
            }
            if(hGC_CounterError.IsAllocated == true)
            {
                hGC_CounterError.Free();
            }
            if(hGC_CarryBorrow.IsAllocated == true)
            {
                hGC_CarryBorrow.Free();
            }
            if(hGC_Timer.IsAllocated == true)
            {
                hGC_Timer.Free();
            }
            this.Close();
        }

        delegate void SetTextDelegate(string str);
        delegate void SetCountDataDelegate(uint[] CntData, uint[] StatusData);

        //---------------------------------------------------------
        // Set NotifyLabel(Synchronous function for control operation)
        //---------------------------------------------------------
        void SetNotifyText(string str)
        {
            lblNotify.Text = str;
        }
        //---------------------------------------------------------
        // Set RetTextBox(Synchronous function for control operation)
        //---------------------------------------------------------
        void SetRetText(string str)
        {
            txtRet.Text = str;
        }
        //---------------------------------------------------------
        // Set count data(Synchronous function for control operation)
        //---------------------------------------------------------
        void SetCountDataText(uint[] CntData, uint[] StatusData)
        {
            string str;
            short  i;

            //------------------------------
            // Display data
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                str = string.Format("{0:D8}", CntData[i]);
                txt_Count[i].Text = str;
                str = string.Format("{0:X8}H", StatusData[i]);
                txt_Status[i].Text = str;
            }
        }
        //---------------------------------------------------------
        // Callback routine
        //---------------------------------------------------------
        unsafe public void SetCountUpCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string    str;
            int        MatchCh;
            MatchCh = (int)(wParam & 0x0000ffff);
            switch (MatchCh)
            {
                case 0:    // Count match: 0CH
                    CountMatch[0]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 0, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[0]);
                    }
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 0, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[0]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 1:    // Count match: 1CH
                    CountMatch[1]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 1, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[1]);
                    }    
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 1, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[1]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 2:    // Count match: 2CH
                    CountMatch[2]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 2, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[2]);
                    }                    
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 2, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[2]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case 3:    // Count match: 3CH
                    CountMatch[3]++;
                    if(((wParam & 0x0ffff0000) >> 16) == 1 )
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 3, UP, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[3]);
                    }                    
                    else
                    {
                        str = string.Format("Event:\nCount Match!\nChannel No.: 3, DOWN, Count: {0:X8}(Hex)\n Matched: {1:d} times", lParam, CountMatch[3]);
                    }
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        unsafe public void CounterErrorCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string    str;
            int        ErrType;
            int        ErrCh;

            ErrType = (int)(lParam & 0x0000ffff);
            ErrCh = (int)(wParam & 0x0000ffff);
            switch (ErrType)
            {
                case (int)CcntConst.CNT_AI_ERROR:        // Abnormal input
                    str = string.Format("Event:\nAbnormal input error!\nChannel No.: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_FLT_ERROR:        // Filter
                    str = string.Format("Event:\nFilter error!\nChannel No.: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_ALM_ERROR:        // Alarm
                    str = string.Format("Event:\nAlarm error!\nChannel No.: {0:d}", ErrCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        unsafe public void CarryBorrowCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            string    str;
            int        NotifyFac;
            int        CarryCh;
            NotifyFac = (int)(lParam & 0x0000ffff);
            CarryCh = (int)(wParam & 0x0000ffff);
            switch (NotifyFac)
            {
                case (int)CcntConst.CNT_CB_CARRY:        // Carry
                    str = string.Format("Event:\nCarry!\nChannel No.: {0:d}", CarryCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
                case (int)CcntConst.CNT_CB_BORROW:        // Borrow
                    str = string.Format("Event:\nBorrow!\nChannel No.: {0:d}", CarryCh);
                    this.Invoke(new SetTextDelegate(SetNotifyText), new Object[] {str});
                    break;
            }

            return;
        }

        //---------------------------------------------------------
        // Consider a device that does not have a timer notification function,
        // we use the window timer.
        // Please switch and use, according to the use.
        //---------------------------------------------------------
        //---------------------------------------------------------
        // Method for window timer
        //---------------------------------------------------------
        private void timer1_Tick(object sender, EventArgs e)
        {
            short[] ChNo = new short[CHANNEL_NUM];
            uint[]  CntData = new uint[CHANNEL_NUM];
            uint[]  StatusData = new uint[CHANNEL_NUM];
            short   i;
            int     ret;
            string  str;
            string  error_string = new string(' ', 256);

            //------------------------------
            // Set parameter
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                ChNo[i] = i;
                CntData[i] = 0;
                StatusData[i] = 0;
            }
            //------------------------------
            // Get count value
            //------------------------------
            ret = cnt.ReadCount(Id, ChNo, ChannelNum, CntData);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntReadCount ret = {0:d}: {1}", ret, error_string);
                txtRet.Text = str;
                timer1.Stop();
            }
            //------------------------------
            // Get count status
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                ret = cnt.ReadStatusEx(Id, i, out StatusData[i]);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntReadStatus ret = {0:d}: {1}", ret, error_string);
                    txtRet.Text = str;
                    timer1.Stop();
                }
            }
            //------------------------------
            // Display data
            //------------------------------
            for (i = 0; i < ChannelNum; i++)
            {
                str = string.Format("{0:D8}", CntData[i]);
                txt_Count[i].Text = str;
                str = string.Format("{0:X8}H", StatusData[i]);
                txt_Status[i].Text = str;
            }
        }

        /*
        //---------------------------------------------------------
        // When using the timer notification event of device,
        // please use the following callback routine.
        //---------------------------------------------------------
        //---------------------------------------------------------
        // Callback routine for timer notification
        //---------------------------------------------------------
        unsafe public void TimerCallBackProc(short Id, int wParam, int lParam, void *Param)
        {
            short[] ChNo = new short[CHANNEL_NUM];
            uint[]  CntData = new uint[CHANNEL_NUM];
            uint[]  StatusData = new uint[CHANNEL_NUM];
            short   i;
            int     ret;
            string  str;
            string  error_string = new string(' ', 256);
            
            //------------------------------
            // Set parameter
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ChNo[i] = i;
                CntData[i] = 0;
                StatusData[i] = 0;
            }
            //------------------------------
            // Get count value
            //------------------------------
            ret = cnt.ReadCount(Id, ChNo, ChannelNum, CntData);
            if (ret != 0) 
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntReadCnt ret = {0:d}: {1}", ret, error_string);
                this.Invoke(new SetTextDelegate(SetRetText), new Object[] {str});
                cnt.StopNotifyTimer(Id);
            }
            //------------------------------
            // Get count status
            //------------------------------
            for (i=0; i<ChannelNum; i++) 
            {
                ret = cnt.ReadStatusEx(Id, i, out StatusData[i]);
                if (ret != 0) 
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntReadStatus ret = {0:d}: {1}", ret, error_string);
                    this.Invoke(new SetTextDelegate(SetRetText), new Object[] {str});
                    cnt.StopNotifyTimer(Id);
                }
            }
            //------------------------------
            // Display data
            //------------------------------
            this.Invoke(new SetCountDataDelegate(SetCountDataText), new Object[] {CntData, StatusData});
            return;
        }
        */
    }
}
