2

I want to break Verification(IAsyncResult r) when status == NBiometricStatus.Ok but it my this code is not breaking my loop, so please kindly help me ...

    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) { }


    }

This is my EndVerify()

public NBiometricStatus EndVerify(IAsyncResult asyncResult);

This is my BeginVerify()

public IAsyncResult BeginVerify(NSubject subject, AsyncCallback callback, object state);
Tharif
  • 13,794
  • 9
  • 55
  • 77

1 Answers1

0

Please check that _mbiometricClient.EndVerify(r); has return result of execution. It seems that EndVerify is still running during check status in the if statement. Its possible that you dont receive the result of a task. My answer is based on assumption that EndVerify is async.

  • Yes.In my code if we hav just only one record in DataBase on that time this code running fine , but when we have multiple record that time in my second function Verification(IAsyncResult r) is not breaking when we get correct result. – P. Katariya Mar 25 '16 at 10:24
  • It should be stop when status come Ok. but it is not happening in my this code. – P. Katariya Mar 25 '16 at 10:26
  • My another question is EndVerify run async? – Arkhangelskiy Evgeniy Mar 25 '16 at 10:57
  • So i think that the root cause is that it run async and it is not awaited. and at the moment status is not equal to `NBiometricStatus.Ok`. I think you should have something like `NBiometricStatus status = await _mbiometricClient.EndVerify(r);` and make changes in EndVerify if needed – Arkhangelskiy Evgeniy Mar 25 '16 at 12:04
  • Giving me error :"The await operation can only used within an async method.Consider marking this method with the async modifier and changing its return type to Task" – P. Katariya Mar 25 '16 at 12:30
  • As i said, you should make changes in EndVerify method, as its run async. Change return type to Task. Its would be better if you share code of EndVerify function. – Arkhangelskiy Evgeniy Mar 25 '16 at 12:37
  • This is my EndVerify Code .public NBiometricStatus EndVerify(IAsyncResult asyncResult); – P. Katariya Mar 26 '16 at 04:10