繁体   English   中英

使用C#连接生物识别设备?

[英]connect biometric device using c#?

我正在尝试在Web应用程序中连接生物识别设备。 我在Windows应用程序中工作正常,但在Web中抛出错误。

Windows-form.cs:

int nPort = Convert.ToInt32(textPort.Text);
                        int nPassword = Convert.ToInt32(textPassword.Text);
                        string strIP = ipAddressControl1.IPAddress.ToString();
                        bRet = axFP_CLOCK.SetIPAddress(ref strIP, nPort, nPassword);
                        if(!bRet)
                        {
                            return;
                        }

表单设计师:

((System.ComponentModel.ISupportInitialize)(this.axFP_CLOCK)).BeginInit();
       this.axFP_CLOCK.Enabled = true;
            this.axFP_CLOCK.Location = new System.Drawing.Point(476, 382);
            this.axFP_CLOCK.Name = "axFP_CLOCK";
            this.axFP_CLOCK.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axFP_CLOCK.OcxState")));
            this.axFP_CLOCK.Size = new System.Drawing.Size(100, 50);
            this.axFP_CLOCK.TabIndex = 11;
            this.axFP_CLOCK.Visible = false;
    ((System.ComponentModel.ISupportInitialize)(this.axFP_CLOCK)).EndInit();

同样,我尝试在Web应用程序中连接,但显示错误:Webform1:

 public AxFP_CLOCKLib.AxFP_CLOCK axFP_CLOCK;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool bRet;
                string ip= "192.168.1.109";
                int nPort = Convert.ToInt32(5005);
                int nPassword = Convert.ToInt32(0);
                axFP_CLOCK = new AxFP_CLOCKLib.AxFP_CLOCK();
                bRet = axFP_CLOCK.SetIPAddress(ref ip, nPort, nPassword);
                if (!bRet)
                {
                    Response.Write("success");
                }

                else
                {
                    Response.Write("failure");
                }
            }
        }

它抛出错误为

由于当前线程不在单线程单元中,因此无法实例化ActiveX控件'87733ee1-d095-442b-a200-6de90c5c8318'。

Dll:

 public virtual bool SetIPAddress(ref string lpszIPAddress, int dwPortNumber, int dwPassWord)
    {
      if (this.ocx == null)
        throw new AxHost.InvalidActiveXStateException(nameof (SetIPAddress), AxHost.ActiveXInvokeKind.MethodInvoke);
      return this.ocx.SetIPAddress(ref lpszIPAddress, dwPortNumber, dwPassWord);
    }

有人可以帮助我纠正此错误吗?

错误消息表明您使用的ActiveX控件需要STA(单线程单元)状态。 您可以使用Thread.SetApartmentState()方法设置相应线程的单元状态。

假设您已经创建了一个Thread来包装ActiveX实例,如下所示:

var thread = new Thread(() => {
    // other running processes inside thread
    Application.Run();
});

然后,您应该在使用Start()之前将该线程的公寓状态指定为STA( ApartmentState.STA Start()

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

此问题背后的原因是,大多数.NET后台线程(或辅助API)将创建新的线程作为多线程单元状态(MTA),而ActiveX控件线程通常以STA模式实例化。

相关问题:

单线程单元-无法实例化ActiveX控件

您应该在Thread运行您的方法。

像下面这样。

var thr = new Thread(new AxFP_CLOCKLib.AxFP_CLOCK().SetIPAddress(ref ip, nPort, nPassword));
    thr.SetApartmentState(ApartmentState.STA);
    thr.Start();

我对此不太确定,但对您的Page_Load注释也应有所帮助。

  [STAThread]
protected void Page_Load(object sender, EventArgs e)
{
 // Code Goes here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM