private void RunRecognizeText(object threadContext) { MappingRecognizeAsyncResult asyncResult = (MappingRecognizeAsyncResult)threadContext; MappingResultState resultState = new MappingResultState(); MappingPropertyBag bag = null; try { bag = this.RecognizeText(asyncResult.Text, asyncResult.Length, asyncResult.Index, asyncResult.Options); } catch (LinguisticException linguisticException) { resultState = linguisticException.ResultState; } asyncResult.SetResult(bag, resultState); // Don't catch any exceptions. try { asyncResult.AsyncCallback(asyncResult); } finally { Thread.MemoryBarrier(); ((ManualResetEvent)asyncResult.AsyncWaitHandle).Set(); } }
/// <summary> /// Waits for the asynchronous operation to complete. /// </summary> /// <param name="asyncResult">The <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object associated with the operation.</param> public static void EndRecognizeText(MappingRecognizeAsyncResult asyncResult) { if (asyncResult != null && !asyncResult.IsCompleted) { asyncResult.AsyncWaitHandle.WaitOne(); } }
/// <summary> /// Calls an ELS service to recognize text. For example, the Microsoft Language Detection service /// will attempt to recognize the language in which the input text is written. The execution will be /// handled in a thread from the managed thread pool, and the asynchronous wait handle of the returned /// <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object will be notified when the operation completes. The results of /// the call will be stored inside the <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object. /// </summary> /// <param name="text">The text to recognize. The text must be UTF-16, but some services have additional /// requirements for the input format. This parameter cannot be set to null.</param> /// <param name="length">Length, in characters, of the text specified in text.</param> /// <param name="index">Index inside the specified text to be used by the service. This value should be /// between 0 and length-1. If the application wants to process the entire text, it should set this /// parameter to 0.</param> /// <param name="options">Optional. A <see cref="MappingOptions">MappingOptions</see> object containing options that affect the result and /// behavior of text recognition. The application does not have to specify values for all object members. /// This parameter can be set to null to use the default mapping options.</param> /// <param name="asyncCallback">An application callback delegate to receive callbacks with the results from /// the recognize operation. Cannot be set to null.</param> /// <param name="callerData">Optional. Private application object passed to the callback function /// by a service after text recognition is complete. The application must set this parameter to null to /// indicate no private application data.</param> /// <returns>A <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object describing the asynchronous operation.</returns> public MappingRecognizeAsyncResult BeginRecognizeText(string text, int length, int index, MappingOptions options, AsyncCallback asyncCallback, object callerData) { if (asyncCallback == null) { throw new ArgumentNullException("asyncCallback"); } MappingRecognizeAsyncResult result = new MappingRecognizeAsyncResult(callerData, asyncCallback, text, length, index, options); ThreadPool.QueueUserWorkItem(this.RunRecognizeText, result); return(result); }
/// <summary> /// Calls an ELS service to recognize text. For example, the Microsoft Language Detection service /// will attempt to recognize the language in which the input text is written. The execution will be /// handled in a thread from the managed thread pool, and the asynchronous wait handle of the returned /// <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object will be notified when the operation completes. The results of /// the call will be stored inside the <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object. /// </summary> /// <param name="text">The text to recognize. The text must be UTF-16, but some services have additional /// requirements for the input format. This parameter cannot be set to null.</param> /// <param name="length">Length, in characters, of the text specified in text.</param> /// <param name="index">Index inside the specified text to be used by the service. This value should be /// between 0 and length-1. If the application wants to process the entire text, it should set this /// parameter to 0.</param> /// <param name="options">Optional. A <see cref="MappingOptions">MappingOptions</see> object containing options that affect the result and /// behavior of text recognition. The application does not have to specify values for all object members. /// This parameter can be set to null to use the default mapping options.</param> /// <param name="asyncCallback">An application callback delegate to receive callbacks with the results from /// the recognize operation. Cannot be set to null.</param> /// <param name="callerData">Optional. Private application object passed to the callback function /// by a service after text recognition is complete. The application must set this parameter to null to /// indicate no private application data.</param> /// <returns>A <see cref="MappingRecognizeAsyncResult">MappingRecognizeAsyncResult</see> object describing the asynchronous operation.</returns> public MappingRecognizeAsyncResult BeginRecognizeText(string text, int length, int index, MappingOptions options, AsyncCallback asyncCallback, object callerData) { if (asyncCallback == null) { throw new ArgumentNullException("asyncCallback"); } MappingRecognizeAsyncResult result = new MappingRecognizeAsyncResult(callerData, asyncCallback, text, length, index, options); try { ThreadPool.QueueUserWorkItem(this.RunRecognizeText, result); return result; } catch { result.Dispose(); throw; } }