Partager via


RecognizerContext.BackgroundRecognizeWithAlternates Method

RecognizerContext.BackgroundRecognizeWithAlternates Method

Causes the Recognizer object to recognize the associated Strokes collection and raise a RecognitionWithAlternates event when recognition is complete.

Definition

Visual Basic .NET Public Sub BackgroundRecognizeWithAlternates()
C# public void BackgroundRecognizeWithAlternates();
Managed C++ public: void BackgroundRecognizeWithAlternates();

Exceptions

ObjectDisposedException Leave Site: The RecognizerContext object is disposed.

Remarks

This method specifies that ink recognition is performed asynchronously.

To perform recognition that includes only the top alternate, call the BackgroundRecognize method.

The RecognitionWithAlternates event is not raised if the recognizer does not recognize any alternates.

Examples

[C#]

This C# example calls the BackgroundRecognizeWithAlternates method on a RecognizerContext, theRecognizerContext, as each stroke is collected. It then displays a string representation of the RecognitionResult object, as it is generated, in a list box, theListBox. The results are made up of all of the RecognitionAlternate objects in descending order of confidence.

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Ink;

class RecognitionWithAlternatesEventForm : System.Windows.Forms.Form
{
    private ListBox listAlternates;
    InkCollector theInkCollector;
    Strokes theStrokes;
    RecognizerContext theRecognizerContext;
    RecognitionResult theRecognitionResult;

    public RecognitionWithAlternatesEventForm()
    {
        // Initialize the form and the text box.
        // Call SuspendLayout while we adjust things.
        this.listAlternates = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        this.listAlternates.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.listAlternates.Location = new System.Drawing.Point(0, 200);
        this.listAlternates.Size = new System.Drawing.Size(350, 120);
        this.ClientSize = new System.Drawing.Size(350, 320);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listAlternates});
        this.Text = "RecognitionWithAlternates Event Demo";
        this.ResumeLayout(false);

        // Create new ink collector and recognizer context
        theInkCollector = new InkCollector(Handle);
        theRecognizerContext = new RecognizerContext();

        // Initialize the recognizer's strokes
        // and assign them to the recognizer context.
        theStrokes = theInkCollector.Ink.Strokes;
        theRecognizerContext.Strokes = theStrokes;

        // Install event handlers.
        theInkCollector.Stroke +=
            new InkCollectorStrokeEventHandler(Stroke_Event);
        theRecognizerContext.RecognitionWithAlternates +=
            new RecognizerContextRecognitionWithAlternatesEventHandler(
            RecognitionWithAlternates_Event);

        // Enable the ink collector
        theInkCollector.Enabled = true;
    }

    public static void Main()
    {
        Application.Run(new RecognitionWithAlternatesEventForm());
    }

    // Stroke event handler
    private void Stroke_Event(object sender,
        InkCollectorStrokeEventArgs e)
    {
        // When a new stroke is collected,
        // add it to the recognizer's strokes collection.
        theStrokes.Add(e.Stroke);
        // Tell the context to recognize its strokes.
        theRecognizerContext.BackgroundRecognizeWithAlternates();
    }

    // Recognition Event Handler
    private void RecognitionWithAlternates_Event(
        object sender,
        RecognizerContextRecognitionWithAlternatesEventArgs e)
    {
        theRecognitionResult = e.Result;
        RecognitionAlternates theRecognitionAlternates =
            theRecognitionResult.GetAlternatesFromSelection(0, -1);
        // Update the text box with the top string of the result
        listAlternates.Items.Clear();
        foreach (RecognitionAlternate theRecognitionAlternate in theRecognitionAlternates)
        {
            listAlternates.Items.Add(theRecognitionAlternate.ToString());
        }
    }

    // Event handler for the form's closed event
    private void RecognitionWithAlternatesEventForm_Closed(object sender, System.EventArgs e)
    {
        theInkCollector.Dispose();
        theInkCollector = null;
        theRecognizerContext.Dispose();
        theRecognizerContext = null;
    }
}

[Visual Basic .NET]

This Microsoft® Visual Basic® .NET example calls the BackgroundRecognizeWithAlternates method on a RecognizerContext, theRecognizerContext, as each stroke is collected. It then displays a string representation of the RecognitionResult object, as it is generated, in a list box, theListBox. The results are made up of all of the RecognitionAlternate objects in descending order of confidence.

Imports Microsoft.Ink
Public Class Form1
    Inherits System.Windows.Forms.Form

'... Code for initializing Form1 and theListBox goes here.

    Dim theInkCollector As InkCollector
    Dim theStrokes As Strokes
    Dim theRecognizerContext As RecognizerContext
    Dim theRecognitionResult As RecognitionResult

    Private Sub Stroke_Event(ByVal sender As Object, _
    ByVal e As InkCollectorStrokeEventArgs)
        'When a new stroke is collected, add it to
        'the RecognizerContext's strokes collection
        theStrokes.Add(e.Stroke)
        'Tell the RecognizerContext to recognize
        theRecognizerContext.BackgroundRecognizeWithAlternates()
    End Sub

    Private Sub RecognizeWithAlternates_Event( _
    ByVal sender As Object, _
    ByVal e As RecognizerContextRecognitionWithAlternatesEventArgs)
        'Clear the list box
        theListBox.Items.Clear()
        'Get the set of alternates from the entire
        'result selection.
        theRecognitionResult = e.Result
        Dim theRecognitionAlternates As RecognitionAlternates
        theRecognitionAlternates = theRecognitionResult.GetAlternatesFromSelection(0, -1)
        'Update the list box with the alternates
        Dim theRecognitionAlternate As RecognitionAlternate
        For Each theRecognitionAlternate In theRecognitionAlternates
            theListBox.Items.Add(theRecognitionAlternate.ToString)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        theInkCollector = New InkCollector(Handle)
        'Create new RecognizerContext
        theRecognizerContext = New RecognizerContext()
        'Initialize the recognizer's strokes
        'and assign them to the RecognizerContext
        theStrokes = theInkCollector.Ink.Strokes
        theRecognizerContext.Strokes = theStrokes
        'Add the handler for the Stroke event
        AddHandler theInkCollector.Stroke, AddressOf Stroke_Event
        'Handle Recognition event
        AddHandler theRecognizerContext.RecognitionWithAlternates, _
        AddressOf RecognizeWithAlternates_Event
        'Enable the ink collector
        theInkCollector.Enabled = True
    End Sub

    ' Event handler for the form's closed event
    Private Sub Form1_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        theInkCollector.Dispose()
        theInkCollector = Nothing
        theRecognizerContext.Dispose()
        theRecognizerContext = Nothing
    End Sub
End Class

See Also