2

当我尝试关闭它抛出的表单时,我正在使用数字角色 SDK 进行此练习

跨线程操作无效:控件从创建它的线程以外的线程访问

此代码用于登录

SqlConnection dataBaseConnection = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=DemoDB;Integrated Security=True");
        public delegate void OnTemplateEventHandler(DPFP.Template template);

        Timer closeTimer = new Timer();
        DPFP.Capture.Capture Capturer = new DPFP.Capture.Capture();
        public login()
        {
            InitializeComponent();
            closeTimer.Tick+=new EventHandler(closeTimer_Tick);
        }

        private DPFP.Template Template;
        private DPFP.Verification.Verification Verificator;
        int time = 0;

        protected virtual void Init()
        {
            Capturer = new DPFP.Capture.Capture();      
            Capturer.EventHandler = this;
            Verificator = new DPFP.Verification.Verification();     // Create a fingerprint template verificator
        }

        protected void start()
        {
            Capturer.StartCapture();
        }

        protected virtual void Process(DPFP.Sample sample)
        {

                drawPicture(convertingToBitmap(sample));

                // Process the sample and create a feature set for the enrollment purpose.
                DPFP.FeatureSet features = ExtractFeatures(sample, DPFP.Processing.DataPurpose.Verification);

                // Check quality of the sample and start verification if it's good
                // TODO: move to a separate task
                if (features != null)
                {
                    // Compare the feature set with our template
                    DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();
                    Verificator.Verify(features, Template, ref result);
                    if (result.Verified)
                    {
                        MessageBox.Show("Login Success.");
                        this.Close(); // This the place where the exception is thrown

                    }
                    else
                        MessageBox.Show("The fingerprint was NOT VERIFIED.");

                }


        }

        protected void stop()
        {
            Capturer.StopCapture();
        }

        protected DPFP.FeatureSet ExtractFeatures(DPFP.Sample Sample, DPFP.Processing.DataPurpose Purpose)
        {
            DPFP.Processing.FeatureExtraction Extractor = new DPFP.Processing.FeatureExtraction();  // Create a feature extractor
            DPFP.Capture.CaptureFeedback feedback = DPFP.Capture.CaptureFeedback.None;
            DPFP.FeatureSet features = new DPFP.FeatureSet();
            Extractor.CreateFeatureSet(Sample, Purpose, ref feedback, ref features);            // TODO: return features as a result?
            if (feedback == DPFP.Capture.CaptureFeedback.Good)
                return features;
            else
                return null;
        }

        private void drawPicture(Bitmap image)
        {
            this.Invoke(new Function(delegate()
            {
                fingerprintImage.Image = new Bitmap(image, fingerprintImage.Size);
            }));
        }

        protected Bitmap convertingToBitmap(DPFP.Sample sample)
        {
            DPFP.Capture.SampleConversion convertor = new DPFP.Capture.SampleConversion();
            Bitmap img = null;
            convertor.ConvertToPicture(sample, ref img);
            return img;
        }

        #region EventHandler Members:

        public void OnComplete(object Capture, string ReaderSerialNumber, DPFP.Sample Sample)
        {
            //MakeReport("The fingerprint sample was captured.");
            Process(Sample);
            closeTimer.Start();
        }

        public void OnFingerGone(object Capture, string ReaderSerialNumber)
        {
            // MakeReport("The finger was removed from the fingerprint reader.");
        }

        public void OnFingerTouch(object Capture, string ReaderSerialNumber)
        {
            // MakeReport("The fingerprint reader was touched.");
        }

        public void OnReaderConnect(object Capture, string ReaderSerialNumber)
        {
            //MakeReport("The fingerprint reader was connected.");
        }

        public void OnReaderDisconnect(object Capture, string ReaderSerialNumber)
        {
            MessageBox.Show("The fingerprint reader was disconnected.");
        }

        public void OnSampleQuality(object Capture, string ReaderSerialNumber, DPFP.Capture.CaptureFeedback CaptureFeedback)
        {
            //if (CaptureFeedback == DPFP.Capture.CaptureFeedback.Good)
            //    MakeReport("The quality of the fingerprint sample is good.");
            //else
            //    MakeReport("The quality of the fingerprint sample is poor.");
        }
        #endregion
4

2 回答 2

0

使用一个微妙的功能

delegate void Function();
this.Invoke(new Function(delegate()
{
   this.Close();
}));

问题解决了。

于 2012-04-01T03:33:51.407 回答
0

只需使用一行代码。我已经在 SDK 中进行了测试。它就像一个魅力!

this.Invoke(new MethodInvoker(this.Close));

所以你的代码将是

如果(结果。已验证)

{

MessageBox.Show("Login Success.");
this.Invoke(new MethodInvoker(this.Close));

}

您可能需要为 MethodInvoker 添加下面提到的命名空间

使用 System.Windows.Forms;

于 2015-10-02T01:59:28.257 回答