Skip to content
CheshireCaat edited this page Feb 7, 2020 · 15 revisions

Run functions with 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.

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

Single function call

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();
}

Parallel function call

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);

Handle errors

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.

Clone this wiki locally