-
Notifications
You must be signed in to change notification settings - Fork 11
Functions (Client)
If you want to run the function, and you do not need to control the lifetime of the threads, use BasRemoteClient instance for this. In this case, the client itself creates and terminates BAS threads to perform functions.
There are two possible ways to start - synchronous and asynchronous. In the case of the asynchronous version, the task will be launched, which will end immediately after receiving the result or error from function. This is very convenient since you can run tasks not only sequentially, but also in parallel. Take a look at examples
using (var client = new BasRemoteClient(new Options {ScriptName = "TestRemoteControl"}))
{
await client.Start();
// Call one function and get the result.
var result = await client.RunFunction("GoogleSearch", new Params
{
{"Query", "cats"}
});
System.Console.ReadKey();
}
var task1 = client.RunFunction("GoogleSearch", new Params {{"Query", "cats"}});
var task2 = client.RunFunction("GoogleSearch", new Params {{"Query", "dogs"}});
var result = await Task.WhenAll(task1, task2);
foreach (var taskResult in result)
foreach (var link in taskResult)
Console.WriteLine(link);
try
{
var result = await client.RunFunction("NotExistingFunction", Params.Empty);
}
catch (FunctionException ex)
{
Console.WriteLine(ex.Message);
}
Other way to run functions is synchronous variant. By calling client.RunFunctionSync
you can specify custom handlers for result and error. In addition, you can control the execution of a function and stop it at any time.
RunFunctionSync
returns interface IBasFunction
that contains Stop()
method. By calling this method you can imeddiately stops function execution.
In the example below, a function is called that takes a very long time, but we stop it after three seconds of waiting.
var function = client.RunFunctionSync("LongRunningFunction", Params.Empty,
result =>
{
// Any actions with result.
},
exception =>
{
// Any actions with error.
});
Thread.Sleep(3000);
function.Stop();