Implement schedule main thread task with return value (#1579)

* Implement schedule main thread task with return value

* Revert change of TestBot.cs
This commit is contained in:
ReinforceZwei 2021-05-12 12:20:13 +08:00 committed by GitHub
parent 073458f5f2
commit f848495243
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 39 deletions

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MinecraftClient
{
public class TaskWithResult
{
private Delegate Task;
private AutoResetEvent ResultEvent = new AutoResetEvent(false);
public object Result;
public TaskWithResult(Delegate task)
{
Task = task;
}
/// <summary>
/// Execute the delegate and set the <see cref="Result"/> property to the returned value
/// </summary>
/// <returns>Value returned from delegate</returns>
public object Execute()
{
Result = Task.DynamicInvoke();
return Result;
}
/// <summary>
/// Block the program execution
/// </summary>
public void Block()
{
ResultEvent.WaitOne();
}
/// <summary>
/// Resume the program execution
/// </summary>
public void Release()
{
ResultEvent.Set();
}
}
}