-
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.
The RunFunction()
method returns an object that implements the IFunction
interface.
Using it, you can stop the execution of a running function, get the ID of the thread in which it was run, or get the function result using methods GetTask()
or GetTask<TResult>()
Look at the 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();