如何打破IAsyncResult?

时间:2016-03-25 09:41:57

标签: c# .net iasyncresult

我希望在Verification(IAsyncResult r)时暂停status == NBiometricStatus.Ok,但我的代码并没有打破我的循环,所以请帮助我......

    private void OnCapturingCompleted(IAsyncResult r)
    {
        var enrollTask = new NBiometricTask(NBiometricOperations.Enroll);

        try
        {
            if (InvokeRequired)
            {
                BeginInvoke(new AsyncCallback(OnCapturingCompleted), r);
            }
            else
            {
                NBiometricStatus status = _mbiometricClient.EndCapture(r);
                // If Stop button was pushed
                if (status == NBiometricStatus.Canceled) return;

                M_lblStatus.Text = status.ToString();
                if (status != NBiometricStatus.Ok)
                {
                    _mbiometricClient.ForceStart();

                }
                else
                {
                    DataTable dt = QueryMaster.GetFaceData();


                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        byte[] b1 = System.Convert.FromBase64String(dt.Rows[i]["F_feature"].ToString());
                        _Rsubject = new NSubject();
                        _Rsubject.SetTemplateBuffer(NBuffer.FromArray(b1));

                        if (_subject != null && _Rsubject != null)
                        {
                            _mbiometricClient.BeginVerify(_subject, _Rsubject, Verification, null);
                            if (result != null)
                            {                                 
                                break;
                            }
                        }

                    }
                }
            }
        }
        catch (Exception ex) { }
        finally
        {
                        }

    }

    private void Verification(IAsyncResult r)
    {
        string msg;
        try
        {
            if (InvokeRequired)
            {
                BeginInvoke(new AsyncCallback(Verification), r);
            }
            else
            {
                try
                #region foreloop
                {

                    foreach (NMatchingResult t in _subject.MatchingResults)
                    {
                        NBiometricStatus status = _mbiometricClient.EndVerify(r);
                        var verificationStatus = string.Format("Verification status: {0}", status);
                        if (status == NBiometricStatus.Ok)
                        {
                            //get matching score
                            int score = _subject.MatchingResults[0].Score;
                            msg = string.Format("Score of matched templates: {0}", score);
                            msg += " " + verificationStatus;
                            lblFinalStatus.Text = msg;
                            //MessageBox.Show(msg);
                            result = msg;
                            //EndInvoke(r);
                            Func<string> function = r.AsyncState as Func<string>;
                            msg = function.EndInvoke(r);                            

                            break;
                        }
                        else
                        {
                            lblFinalStatus.Text = verificationStatus;
                            count += 1;
                            if (count == 10)
                            {
                                MessageBox.Show(string.Format("{0}\n{1}", verificationStatus, "Face doesn't Exist..!"));
                            }
                            _mbiometricClient.ForceStart();
                        }
                    }

                }
                catch (Exception ex)
                {
                    Utils.ShowException(ex);
                }
                #endregion

            }
            if (result != null)
            {

                Func<string> function = r.AsyncState as Func<string>;
                msg = function.EndInvoke(r);
            }
        }
        catch (Exception ex) { }


    }

这是我的EndVerify()

public NBiometricStatus EndVerify(IAsyncResult asyncResult);

这是我的BeginVerify()

public IAsyncResult BeginVerify(NSubject subject, AsyncCallback callback, object state);

1 个答案:

答案 0 :(得分:0)

请检查_mbiometricClient.EndVerify(r);是否已返回执行结果。似乎EndVerify在if语句的check status期间仍在运行。你可能没有收到任务的结果。我的回答是基于EndVerify是异步的假设。