Asynchronous thread optimization

This commit is contained in:
skx 2021-01-22 16:09:17 +08:00
parent 845dbd99e9
commit 634867d495
2 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic; using System.Dynamic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -748,8 +749,17 @@ namespace SqlSugar
} }
else else
{ {
IsSingleInstance = true; StackTrace st = new StackTrace(true);
result = NoSameThread(); var methods = st.GetFrames();
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
if (isAsync)
{
result = Synchronization();
}
else
{
result = NoSameThread();
}
} }
if (result.Root == null) if (result.Root == null)
{ {

View File

@ -7,6 +7,7 @@ using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -44,6 +45,29 @@ namespace SqlSugar
} }
return value; return value;
} }
public static bool IsAnyAsyncMethod(StackFrame[] methods)
{
bool isAsync = false;
foreach (var item in methods)
{
if (UtilMethods.IsAsyncMethod(item.GetMethod()))
{
isAsync = true;
}
}
return isAsync;
}
public static bool IsAsyncMethod(MethodBase method)
{
if (method == null)
{
return false;
}
Type attType = typeof(AsyncStateMachineAttribute);
var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(attType);
return (attrib != null);
}
public static StackTraceInfo GetStackTrace() public static StackTraceInfo GetStackTrace()
{ {