Skip to content

Is there a way to check if the Engine has finished executing? #715

@DavidBal

Description

@DavidBal

Hi ClearScript-Team,

since ClearScript 7.5 the engine no longer returns a promise/task if the last Statement is a not awaited promise/task.

We have some thing like this code:

async function test()
{
    let text = await File.readAllTextAsync("C:\\tmp\\file.txt");
    DebugOutput(`1. Read text from file: ${text}`);
    text = await File.readAllTextAsync("C:\\tmp\\file.txt");
    DebugOutput(`2. Read text from file: ${text}`);
    text = await File.readAllTextAsync("C:\\tmp\\file.txt");
    DebugOutput(`3. Read text from file: ${text}`);
    text = await File.readAllTextAsync("C:\\tmp\\file.txt");
    DebugOutput(`4. Read text from file: ${text}`);
}

test();

Is there a way to check if the engine is still executing (async) code?

Full C# Example:

internal class Program
{
    static void Main(string[] args)
    {

        string scriptCode = """
            async function test()
            {
                for (let i = 0; i < 100; i++)
                {
                    console.log(`Starting iteration ${i}`);
                    Example.DebugOutput(`Iteration ${i}`);
                    const text = await Example.readAllTextAsync("C:\\tmp\\file.txt");
                    Example.DebugOutput(`Iteration ${i} - File content: ${text}`);
                }
            }

            test();
            """;

        
        using(V8ScriptEngine scriptEngine = new V8ScriptEngine("Test", V8ScriptEngineFlags.EnableTaskPromiseConversion)) 
        {
            scriptEngine.AddHostType("Example", typeof(Example));

            object result = scriptEngine.Evaluate(new DocumentInfo(new Uri("c:\\temp\\fake.js")) {
                Category = ModuleCategory.Standard
            }, scriptCode);

            if(result is Task t)
            {
                t.GetAwaiter().GetResult();
            }

            Console.WriteLine("Script execution completed.");
        }

    }
}

public static class Example
{
    public static async Task<string> readAllTextAsync(string path)
    {
        return await File.ReadAllTextAsync(path);
    }

    public static void DebugOutput(string text)
    {
        Console.WriteLine(text);
    }
}

Output:

Iteration 0
Iteration 0 - File content: Das ist ein Test Text 123 1!
Iteration 1
Iteration 1 - File content: Das ist ein Test Text 123 1!
Iteration 2
Iteration 2 - File content: Das ist ein Test Text 123 1!
Iteration 3
Script execution completed.
Iteration 3 - File content: Das ist ein Test Text 123 1!
Iteration 4

I already have checked this issues:
#672
#649

Kind regards,
David

EDIT: Full C# Example.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions