Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, April 20, 2017 10:47:19 AM(UTC)

Nick  
Nick

Groups: Registered, Tech Support, Administrators
Posts: 42


Some LEADTOOLS higher-order methods accept a method as a parameter. This code tips shows how anonymous methods can be inlined in the call. This example uses the call to IOcrDocument.Recognize(), however, it is applicable to and can be modified to accommodate any LEADTOOLS higher-order method which can accept a delegate.

Typically, the call to Recognize() is made such:

Code:

IOcrDocument.Pages.Recognize(null);


The passed parameter--null in this case--is the method called when the procedure for Recognize() updates its internal status. Passing in null simply means Recognize() will not provide any updates until it completes. Note the parameter expected is either null or the delegate type OcrProgressCallback. The OcrProgressCallback type is a delegate type with a return type of void that accepts exactly one parameter of IOcrProgressData:

Code:

public delegate void OcrProgressCallback(IOcrProgressData data);


Any method which conforms to this signature can be used as an OcrProgressCallback, similar to how any integer value can be used as an integer. Here's how a method can be explicitly passed in as a parameter:

Code:

IOcrDocument.Pages.Recognize(new OcrProgressCallback(MyOcrProgressCallback));


This passes to Recognize() a new OcrProgressCallback object which refers to a method with the name MyOcrProgressCallback. Here's the method contents--note how the signature matches the delegate type.

Code:

private void MyOcrProgressCallback(IOcrProgressData data) 
{ 
   if (data.Percentage == 0) 
      Console.WriteLine("--------------------------"); 
 
   Console.WriteLine("Page:{0}({1}:{2}) {3}% Operation:{4}", 
      data.CurrentPageIndex.ToString("00"), 
      data.FirstPageIndex.ToString("00"), 
      data.LastPageIndex.ToString("00"), 
      data.Percentage.ToString("000"), 
      data.Operation); 
}


This method will be executed each time Recognize() reports its progress. For a complete code sample, see our documentation page on the OcrProgressCallback delegate.
https://www.leadtools.com/help/leadtools/v19m/dh/fo/ocrprogresscallback.html

It's also possible to inline the method call anonymously--that is, without having a distinct method name--by declaring it and its contents directly in the constructor for OcrProgressCallback. Here's how this can be done using the existing method contents.

Code:

IOcrDocument.Pages.Recognize(new OcrProgressCallback(
                  delegate(IOcrProgressData data)
                  {
                     if (data.Percentage == 0)
                        Console.WriteLine("--------------------------");

                     Console.WriteLine("Page:{0}({1}:{2}) {3}% Operation:{4}",
                        data.CurrentPageIndex.ToString("00"),
                        data.FirstPageIndex.ToString("00"),
                        data.LastPageIndex.ToString("00"),
                        data.Percentage.ToString("000"),
                        data.Operation);
                  }));


The delegate keyword is required as it instructs the compiler that what follows is a delegate type which matches the signature of OcrProgressCallback. The parameter list, in paranthesis, follows the delegate keyword. After this, the method logic to be invoked is included within the curly braces. The final two parantheses at the bottom close the constructor for OcrProgressCallback and the call to Recognize() respectively.

Inlining methods anonymously in this manner can be useful for very simple events--in this case, simply printing the status to the console--and when the code executed is specific to that method. For logic which is more complex or when the logic will be repeated in multiple places, it may be more practical to use a traditional named method instead, which would be up to the discretion of the developer.

There is one caveat when using anonymous methods. When using Visual Studio in the edit-and-continue mode, neither the anonymous method--nor the method in which it is declared--can be modified when program execution is halted at a breakpoint. Keep this in mind when deciding to inline an anonymous method.

For more information on delegates, see this information available from MSDN.
https://msdn.microsoft.com/en-us/library/ms173171.aspx
Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.
nick@leadtools.com
www.LEADTOOLS.com
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2017, Yet Another Forum.NET
This page was generated in 0.170 seconds.