Share via


RecognizerContext.BackgroundRecognize Method

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

Namespace:  Microsoft.Ink
Assembly:  Microsoft.Ink (in Microsoft.Ink.dll)

Syntax

'Declaration
Public Sub BackgroundRecognize
'Usage
Dim instance As RecognizerContext

instance.BackgroundRecognize()
public void BackgroundRecognize()
public:
void BackgroundRecognize()
public function BackgroundRecognize()

Remarks

This method specifies that ink recognition is performed asynchronously. To recognize ink synchronously, call the RecognizerContext.Recognize method.

This method recognizes only the best result string. This method does not create RecognitionAlternate objects. To perform recognition that creates a list of available alternates, call the BackgroundRecognizeWithAlternates method.

The Recognition event is not raised if the recognizer does not recognize anything.

Examples

In this example, each stroke made in an InkOverlay object is automatically recognized and the recognition result displayed.

During application startup, the RecognizerContext object is instantiated, and event handlers are assigned.

' create a new RecognizerContext object 
' the object's Strokes property is initialized to null
mRecognizerContext = New RecognizerContext()
' assign the Strokes property by creating a fresh Strokes collection 
mRecognizerContext.Strokes = mInkOverlay.Ink.CreateStrokes()
' subscribe to the Strokes event. It is during this event 
' that we can add strokes to the RecognizerContext 
AddHandler mInkOverlay.Stroke, New InkCollectorStrokeEventHandler(AddressOf mInkOverlay_Stroke2)
' subscribe to the the Recognition event.  
' This event is fired when background recognition is complete,  
' and recognition results (without alternates) are available 
AddHandler mRecognizerContext.Recognition, _
        New RecognizerContextRecognitionEventHandler(AddressOf mRecognizerContext_Recognition)
// create a new RecognizerContext object 
// the object's Strokes property is initialized to null
mRecognizerContext = new RecognizerContext();
// assign the Strokes property by creating a fresh Strokes collection 
mRecognizerContext.Strokes = mInkOverlay.Ink.CreateStrokes();
// subscribe to the Strokes event. It is during this event 
// that we can add strokes to the RecognizerContext
mInkOverlay.Stroke += new InkCollectorStrokeEventHandler(mInkOverlay_Stroke2);
// subscribe to the the Recognition event.  
// This event is fired when background recognition is complete,  
// and recognition results (without alternates) are available
mRecognizerContext.Recognition += 
    new RecognizerContextRecognitionEventHandler(mRecognizerContext_Recognition);

When the Stroke event fires (in response to the user completing a stroke), the newly created stroke is added to the Strokes collection of the RecognizerContext object, and the BackgroundRecognize method is called.

Private Sub mInkOverlay_Stroke2(ByVal sender As Object, ByVal e As InkCollectorStrokeEventArgs)
    ' in case background recognition is still occurring, stop it
    mRecognizerContext.StopBackgroundRecognition()
    ' add the stroke, and start recognition
    mRecognizerContext.Strokes.Add(e.Stroke)
    mRecognizerContext.BackgroundRecognize()
End Sub
private void mInkOverlay_Stroke2(object sender, InkCollectorStrokeEventArgs e)
{
    // in case background recognition is still occurring, stop it
    mRecognizerContext.StopBackgroundRecognition();
    // add the stroke, and start recognition
    mRecognizerContext.Strokes.Add(e.Stroke);
    mRecognizerContext.BackgroundRecognize();
}

When background recognition is complete, the Recognition event fires. During handling of this event, the results of the recognition are placed in a list box.

' event fires when recognition results (without alternates) are ready 
Private Sub mRecognizerContext_Recognition(ByVal sender As Object, _
            ByVal e As RecognizerContextRecognitionEventArgs)
    ' when updating a control, must use Invoke() since controls are 
    ' not thread safe and recognition occurs on a different thread 
    If Me.InvokeRequired Then 
        ' recursively call this method via Invoke() 
        Me.Invoke( _
            New RecognizerContextRecognitionEventHandler(AddressOf mRecognizerContext_Recognition), _
            New Object() {sender, e} _
        )
        Return 
    End If 
    If RecognitionStatus.NoError = e.RecognitionStatus Then
        listBoxRecognitionResults.Items.Add(e.Text)
    End If 
End Sub
// event fires when recognition results (without alternates) are ready 
private void mRecognizerContext_Recognition(object sender, RecognizerContextRecognitionEventArgs e)
{
    // when updating a control, must use Invoke() since controls are 
    // not thread safe and recognition occurs on a different thread 
    if (this.InvokeRequired)
    {
        // recursively call this method via Invoke() 
        this.Invoke(
            new RecognizerContextRecognitionEventHandler(mRecognizerContext_Recognition), 
            new object[] { sender, e }
            );
        return;
    }
    if (RecognitionStatus.NoError == e.RecognitionStatus)
    {
        listBoxRecognitionResults.Items.Add(e.Text);
    }
}

Platforms

Windows 7, Windows Vista, Windows Server 2008 R2, Windows Server 2008

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Version Information

.NET Framework

Supported in: 3.0

See Also

Reference

RecognizerContext Class

RecognizerContext Members

BackgroundRecognize Overload

Microsoft.Ink Namespace

RecognizerContext.BackgroundRecognizeWithAlternates

ExtendedProperty.Data

RecognitionAlternate