Function Examples

To run these examples, create a project similar to the Getting Started project and replace the "udf_" function with the ones below.


Example 1

This function will run instantly and will run on the main Excel thread while it is calculating.
This function takes a text argument and returns a string concatenation.

public static string udf_SOMETEXT(string sArgument1) 
{
   return "text: " + aArgument1;
}

Example 2

This function will take a few seconds to run and will run on the main Excel thread while it is calculating.
This function will lock the Excel session.

public static string udf_SOMETEXT2() 
{
   string sText = "";
   for (int icount = 1; icount <= 200000; icount++)
   {
      sText = sText + "a";
   }
   return "some text";
}

Example 3

This function will take a few seconds to run and will run on a different thread while it is calculating.
This function will not lock the Excel session.
While this function is calculating "#N/A" will be displayed in the cell.

public static object udf_SOMETEXT3() 
{
   return ExcelDna.Integration.ExcelAsyncUtil.Run("udf_SOMETEXT3",
                                                  null,
   delegate {
      string sText = "";
      for (int icount = 1; icount <= 200000; icount++)
      {
         sText = sText + "a";
      }
      return "some text";
   });
}

Example 4

This function will take 3 seconds to run and will run on a different thread while it is calculating.
This function will not lock the Excel session.
While this function is calculating "#N/A" will be displayed in the cell.

public static object udf_SOMETEXT4(string sArgument1) 
{
   return ExcelDna.Integration.ExcelAsyncUtil.Run("udf_SOMETEXT4",
                                                  sArgument1,
   delegate {
      System.Threading.Thread.Sleep(3000);
      return sArgument1;
   });
}

Example 5 (Async / Await)

link - docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=netframework-4.5
This function calls a method that returns a Task




public static async void udf_SOMETEXT5(string sArgument1, 
                          ExcelAsyncHandle asyncHandle)
{
   try
   {
      object result = await Method_ToCall(sArgument1);
      asyncHandle.SetResult(result);
   }
   catch (System.Exception ex)
   {
      asyncHandle.SetException(ex);
   }
}

static async Task<object> Method_ToCall(string sArgument1)
{
   await
   return "some text";
}

Example 6 (Async / Await)

link - pluralsight.com/guides/using-task-run-async-await
This function calls a method (with no parameters) that does not return a Task

public static async void udf_SOMETEXT5(string sArgument1, 
                          ExcelAsyncHandle asyncHandle)
{
   try
   {
      object result = await Task.Run( Method_ToCall );
      asyncHandle.SetResult(result);
   }
   catch (System.Exception ex)
   {
      asyncHandle.SetException(ex);
   }
}

static Method_ToCall(string sArgument1)
{
   await
   return "some text";
}

Example 7 (Async / Await)

link - pluralsight.com/guides/using-task-run-async-await
This function calls a method (with one parameter) that does not return a Task

public static async void udf_SOMETEXT5(string sArgument1, 
                          ExcelAsyncHandle asyncHandle)
{
   try
   {
      object result = await Task.Run( () => Method_ToCall(sArgument1) );
      asyncHandle.SetResult(result);
   }
   catch (System.Exception ex)
   {
      asyncHandle.SetException(ex);
   }
}

static Method_ToCall(string sArgument1)
{
   await
   return "some text";
}


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext