Author Topic: async or not async, that is the question...  (Read 5311 times)

Serge

  • Newbie
  • *
  • Posts: 12
async or not async, that is the question...
« on: March 24, 2024, 12:15:17 PM »
Hello,

I'd like to know whether the VA_Invoke1 function is called by VoiceAttack in synchronous or asynchronous mode.

In other words, is the function called in fire-and-forget mode or is there a .GetAwaiter().GetResult()?

Best regards

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 264
  • Upstanding Lunatic
    • My AVCS Homepage
Re: async or not async, that is the question...
« Reply #1 on: March 24, 2024, 12:40:55 PM »
Asynchronous.  BUT - if you want to wait for the invocation to complete, you can use the option in the "Execute an External Plugin Function" action:

Quote
If you did not check the, 'wait until function returns' checkbox, the call to the plugin will
be tossed into another thread and VoiceAttack will continue processing actions
immediately.
If you do check the, 'wait until function returns' checkbox, VoiceAttack will do just that...
wait until your code finishes before continuing. At this point you have a chance to work
with any of the values you had modified/added in subsequent actions within the command.
For instance, maybe your plugin goes out to the internet to retrieve information. Your
plugin can set that information to a value that is passed back to VoiceAttack and then
VoiceAttack can use that value (for TTS or for conditional flow)
(see VoiceAttack Manual around page 195)


you could also add something like this within your plugin, if you needed to fire some final code before the plugin exits following this plugin context action:

Code: (C#) [Select]
using System.Threading.Tasks;

private static ManualResetEventSlim _asyncOperationCompleted = new ManualResetEventSlim(false);

private static async void PluginContext(dynamic vaProxy)
{
// do stuff here...


// Signal that this async operation is complete
_asyncOperationCompleted.Set();
}

public static void VA_Invoke1(dynamic vaProxy)
{
// Call async plugin context processing operation on a new thread
var task = Task.Run(() =>
{
PluginContext(vaProxy);
});

// Wait for the async operation to complete
_asyncOperationCompleted.Wait();

// Reset the ManualResetEventSlim for the next async operation
_asyncOperationCompleted.Reset();


// do stuff here after this plugin context is completed...
}

Serge

  • Newbie
  • *
  • Posts: 12
Re: async or not async, that is the question...
« Reply #2 on: March 24, 2024, 01:15:18 PM »
Okay, thanks,

I had read that part of the help doc, but it doesn't clearly indicate whether the call is asynchronous or not, code-wise.

Thanks for your code example, but I'd rather declare VA_Invoke1 private static async since I make a lot of OpenAI API calls and file accesses. If it's handled properly above, everyone wins.

Best regards

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 264
  • Upstanding Lunatic
    • My AVCS Homepage
Re: async or not async, that is the question...
« Reply #3 on: March 26, 2024, 10:49:05 AM »
... I'd rather declare VA_Invoke1 private static async ...

Your .dll will not be recognized as a VoiceAttack plugin if you declare VA_Invoke1 as private.