You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, after I search of 'async await' in issues, with #514#295#52
I know how to do a callback when lua invoke a async C# method with ContinueWith.
But, it is really a 'fire and forgot' invocation. The call stack will not back to the top caller.
How can I do a real one await when invoking DoString() ?
Here is codes reproduce it:
publicasyncTask<(string?,string?)>CreateNextProjectTaskAsync(ProjectTaskupstream,stringnextTaskType){awaitTask.Delay(1000);return(upstream.TaskName,nextTaskType);}publicvoidCreateNextTask(ProjectTaskupstream,stringnextTaskType,LuaFunction?callback=null){CreateNextProjectTaskAsync(upstream,nextTaskType).ContinueWith(t =>{varres=t.Result;callback?.Call(res.Item1,res.Item2);});}[Fact]publicasyncTaskHandleTaskFinished(){varaction=@"import 'System'm:CreateNextTask(task,'Test',function(res1,res2) print(res1) x=res1 print(res2) y=res1end)";usingvarlua=newLua();lua.State.Encoding=Encoding.UTF8;lua.LoadCLRPackage();lua["m"]=this;lua["task"]=newProjectTask(Guid.NewGuid()){TaskName="Test"};varactionResults=lua.DoString(action);varx=lua["x"];//null!! This line run before lua callback!//x.ShouldBe("Test");vary=lua["y"];//y.ShouldBe("Test");awaitTask.Delay(1000);//thread switch to callback invokelua.State.Status.ShouldBe(KeraLua.LuaStatus.OK);}
ProjectTask could be any class with a Guid Id and string TaskName.
The text was updated successfully, but these errors were encountered:
And now, I can use TaskCompletionSource to do some workaround...
privateTaskCompletionSource_taskCompletionSource=new();// one time per await
...
public void CreateNextTask(ProjectTaskupstream,stringnextTaskType,LuaFunction?callback=null){CreateNextProjectTaskAsync(upstream,nextTaskType).ContinueWith(t =>{varres=t.Result;callback?.Call(res.Item1,res.Item2);_taskCompletionSource.SetResult();});}[Fact]
public async Task HandleTaskFinished(){
...var actionResults =lua.DoString(action);await_taskCompletionSource.Task;// wait until _taskCompletionSource.SetResult(); called.var x =lua["x"];// Testx.ShouldBe("Test");var y =lua["y"];// Testy.ShouldBe("Test");
lua.State.Status.ShouldBe(KeraLua.LuaStatus.OK);}
Hi, after I search of 'async await' in issues, with #514 #295 #52
I know how to do a callback when lua invoke a async C# method with
ContinueWith
.But, it is really a 'fire and forgot' invocation. The call stack will not back to the top caller.
How can I do a real one
await
when invokingDoString()
?Here is codes reproduce it:
ProjectTask
could be any class with aGuid Id
andstring TaskName
.The text was updated successfully, but these errors were encountered: