mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Fix calling schedule task cause deadlock (#1586)
This commit is contained in:
parent
7723ed57ea
commit
b15c3a8e46
4 changed files with 55 additions and 6 deletions
|
|
@ -701,13 +701,38 @@ namespace MinecraftClient
|
||||||
/// <param name="task">Task to run</param>
|
/// <param name="task">Task to run</param>
|
||||||
public object ScheduleTask(Delegate task)
|
public object ScheduleTask(Delegate task)
|
||||||
{
|
{
|
||||||
var taskAndResult = new TaskWithResult(task);
|
if (!InvokeRequired())
|
||||||
lock (threadTasksLock)
|
|
||||||
{
|
{
|
||||||
threadTasks.Enqueue(taskAndResult);
|
return task.DynamicInvoke();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var taskAndResult = new TaskWithResult(task);
|
||||||
|
lock (threadTasksLock)
|
||||||
|
{
|
||||||
|
threadTasks.Enqueue(taskAndResult);
|
||||||
|
}
|
||||||
|
taskAndResult.Block();
|
||||||
|
return taskAndResult.Result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if calling thread is main thread or other thread
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if calling thread is other thread</returns>
|
||||||
|
public bool InvokeRequired()
|
||||||
|
{
|
||||||
|
int callingThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
if (handler != null)
|
||||||
|
{
|
||||||
|
return handler.GetNetReadThreadId() != callingThreadId;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// net read thread (main thread) not yet ready
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
taskAndResult.Block();
|
|
||||||
return taskAndResult.Result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Management: Load/Unload ChatBots and Enable/Disable settings
|
#region Management: Load/Unload ChatBots and Enable/Disable settings
|
||||||
|
|
|
||||||
|
|
@ -201,6 +201,15 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
netRead.Start();
|
netRead.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get net read thread (main thread) ID
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Net read thread ID</returns>
|
||||||
|
public int GetNetReadThreadId()
|
||||||
|
{
|
||||||
|
return netRead != null ? netRead.ManagedThreadId : -1;
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
PacketTypePalette packetPalette;
|
PacketTypePalette packetPalette;
|
||||||
SocketWrapper socketWrapper;
|
SocketWrapper socketWrapper;
|
||||||
DataTypes dataTypes;
|
DataTypes dataTypes;
|
||||||
Thread netRead;
|
Thread netRead; // main thread
|
||||||
ILogger log;
|
ILogger log;
|
||||||
|
|
||||||
public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo forgeInfo)
|
public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo forgeInfo)
|
||||||
|
|
@ -1112,6 +1112,15 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
netRead.Start();
|
netRead.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get net read thread (main thread) ID
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Net read thread ID</returns>
|
||||||
|
public int GetNetReadThreadId()
|
||||||
|
{
|
||||||
|
return netRead != null ? netRead.ManagedThreadId : -1;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disconnect from the server, cancel network reading.
|
/// Disconnect from the server, cancel network reading.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -235,5 +235,11 @@ namespace MinecraftClient.Protocol
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="selectedSlot">The slot of the trade, starts at 0.</param>
|
/// <param name="selectedSlot">The slot of the trade, starts at 0.</param>
|
||||||
bool SelectTrade(int selectedSlot);
|
bool SelectTrade(int selectedSlot);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get net read thread (main thread) ID
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Net read thread ID</returns>
|
||||||
|
int GetNetReadThreadId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue