Skip to content
Permalink
Browse files

Better exception handling implementation

  • Loading branch information
ManlyMarco committed Nov 26, 2019
1 parent 6cd69ea commit 3d0a31551c6c4a0711059eed1001c508e1478117
Showing with 19 additions and 11 deletions.
  1. +19 −11 BepInEx/ThreadingHelper.cs
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using BepInEx.Logging;
using UnityEngine;
@@ -116,14 +115,21 @@ private static void LogInvocationException(Exception ex)

IAsyncResult ISynchronizeInvoke.BeginInvoke(Delegate method, object[] args)
{
var result = new InvokeResult();

object Invoke()
{
try { return method.DynamicInvoke(args); }
catch (Exception ex) { return new TargetInvocationException(ex); }
try
{
return method.DynamicInvoke(args);
}
catch (Exception ex)
{
result.ExceptionThrown = true;
return ex;
}
}

var result = new InvokeResult();

if (!InvokeRequired)
result.Finish(Invoke(), true);
else
@@ -134,11 +140,12 @@ object Invoke()

object ISynchronizeInvoke.EndInvoke(IAsyncResult result)
{
result.AsyncWaitHandle.WaitOne();

if (result.AsyncState is TargetInvocationException ex)
throw ex;
return result.AsyncState;
var invokeResult = (InvokeResult)result;
invokeResult.AsyncWaitHandle.WaitOne();

if (invokeResult.ExceptionThrown)
throw (Exception)invokeResult.AsyncState;
return invokeResult.AsyncState;
}

object ISynchronizeInvoke.Invoke(Delegate method, object[] args)
@@ -163,7 +170,7 @@ public InvokeResult()

public void Finish(object result, bool completedSynchronously)
{
AsyncState = result;
AsyncState = result;
CompletedSynchronously = completedSynchronously;
IsCompleted = true;
((EventWaitHandle)AsyncWaitHandle).Set();
@@ -173,6 +180,7 @@ public void Finish(object result, bool completedSynchronously)
public WaitHandle AsyncWaitHandle { get; }
public object AsyncState { get; private set; }
public bool CompletedSynchronously { get; private set; }
internal bool ExceptionThrown;
}

#endregion

0 comments on commit 3d0a315

Please sign in to comment.
You can’t perform that action at this time.