mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
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:
parent
073458f5f2
commit
f848495243
4 changed files with 88 additions and 39 deletions
47
MinecraftClient/TaskWithResult.cs
Normal file
47
MinecraftClient/TaskWithResult.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue