优化线程上下文转换

This commit is contained in:
Kimdiego2098
2023-10-26 19:16:24 +08:00
parent fdf0ba6318
commit d317d757d7
3 changed files with 16 additions and 14 deletions

View File

@@ -518,7 +518,7 @@ public class OPCUAClient : IDisposable
/// </summary>
public async Task ConnectAsync()
{
await ConnectAsync(OPCNode.OPCUrl).ConfigureAwait(false);
await ConnectAsync(OPCNode.OPCUrl);
}
/// <summary>
@@ -547,6 +547,7 @@ public class OPCUAClient : IDisposable
throw new ArgumentNullException("未初始化配置");
}
var useSecurity = OPCNode?.IsUseSecurity ?? true;
EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint(m_configuration, serverUrl, useSecurity, 10000);
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(m_configuration);
ConfiguredEndpoint endpoint = new(null, endpointDescription, endpointConfiguration);
@@ -560,7 +561,7 @@ public class OPCUAClient : IDisposable
userIdentity = new UserIdentity(new AnonymousIdentityToken());
}
//创建本地证书
await m_application.CheckApplicationInstanceCertificate(true, 0, 1200).ConfigureAwait(false);
await m_application.CheckApplicationInstanceCertificate(true, 0, 1200);
m_session = await Opc.Ua.Client.Session.Create(
m_configuration,
endpoint,
@@ -569,7 +570,8 @@ public class OPCUAClient : IDisposable
(string.IsNullOrEmpty(OPCUAName)) ? m_configuration.ApplicationName : OPCUAName,
60000,
userIdentity,
Array.Empty<string>()).ConfigureAwait(false);
Array.Empty<string>()
).ConfigureAwait(false);
typeSystem = new ComplexTypeSystem(m_session);
m_session.KeepAliveInterval = OPCNode.KeepAliveInterval == 0 ? 60000 : OPCNode.KeepAliveInterval;
@@ -739,7 +741,7 @@ public class OPCUAClient : IDisposable
NodeId nodeToRead = new(nodeIdStr);
var node = (VariableNode)await m_session.ReadNodeAsync(nodeToRead, NodeClass.Variable, false, cancellationToken);
if (OPCNode.LoadType)
await typeSystem.LoadType(node.DataType);
await typeSystem.LoadType(node.DataType).ConfigureAwait(false);
_variableDicts.AddOrUpdate(nodeIdStr, node);
return node;
}
@@ -779,7 +781,7 @@ public class OPCUAClient : IDisposable
if (StatusCode.IsGood(nodes.Item2[i].StatusCode))
{
var node = ((VariableNode)nodes.Item1[i]);
await typeSystem.LoadType(node.DataType);
await typeSystem.LoadType(node.DataType).ConfigureAwait(false);
_variableDicts.AddOrUpdate(nodeIdStrs[i], node);
}
else

View File

@@ -164,7 +164,7 @@ public class CollectDeviceThread : IAsyncDisposable
var stoppingToken = StoppingTokens.Last().Token;
DeviceTask = await Task.Factory.StartNew(async () =>
{
await Task.Yield();
//await Task.Yield();
var channelResult = CollectDeviceCores.FirstOrDefault().Driver.GetShareChannel();
LoggerGroup log = CollectDeviceCores.FirstOrDefault().Driver.LogMessage;
foreach (var device in CollectDeviceCores)
@@ -181,11 +181,11 @@ public class CollectDeviceThread : IAsyncDisposable
device.IsShareChannel = CollectDeviceCores.Count > 1;
if (channelResult.IsSuccess)
{
await device.BeforeActionAsync(stoppingToken, channelResult.Content).ConfigureAwait(false);
await device.BeforeActionAsync(stoppingToken, channelResult.Content);
}
else
{
await device.BeforeActionAsync(stoppingToken).ConfigureAwait(false);
await device.BeforeActionAsync(stoppingToken);
}
}
@@ -203,7 +203,7 @@ public class CollectDeviceThread : IAsyncDisposable
//如果是共享通道类型,需要每次转换时切换适配器
if (device.IsShareChannel) device.Driver.InitDataAdapter();
var result = await device.RunActionAsync(stoppingToken).ConfigureAwait(false);
var result = await device.RunActionAsync(stoppingToken);
if (result == ThreadRunReturn.None)
{
await Task.Delay(CycleInterval);
@@ -243,7 +243,7 @@ public class CollectDeviceThread : IAsyncDisposable
{
//如果插件还没释放,执行一次结束函数
if (!device.Driver.DisposedValue)
await device.FinishActionAsync().ConfigureAwait(false);
await device.FinishActionAsync();
}
}

View File

@@ -153,7 +153,7 @@ public class UploadDeviceThread : IAsyncDisposable
var stoppingToken = StoppingTokens.Last().Token;
DeviceTask = await Task.Factory.StartNew(async () =>
{
await Task.Yield();
//await Task.Yield();
LoggerGroup log = UploadDeviceCores.FirstOrDefault().Driver.LogMessage;
foreach (var device in UploadDeviceCores)
{
@@ -165,7 +165,7 @@ public class UploadDeviceThread : IAsyncDisposable
//添加通道报文到每个设备
var data = new EasyLogger(device.Driver.NewMessage) { LogLevel = ThingsGateway.Foundation.Core.LogLevel.Trace };
log.AddLogger(data);
await device.BeforeActionAsync(stoppingToken).ConfigureAwait(false);
await device.BeforeActionAsync(stoppingToken);
}
while (!stoppingToken.IsCancellationRequested)
@@ -180,7 +180,7 @@ public class UploadDeviceThread : IAsyncDisposable
if (device.IsInitSuccess)
{
var result = await device.RunActionAsync(stoppingToken).ConfigureAwait(false);
var result = await device.RunActionAsync(stoppingToken);
if (result == ThreadRunReturn.None)
{
await Task.Delay(CycleInterval);
@@ -221,7 +221,7 @@ public class UploadDeviceThread : IAsyncDisposable
{
//如果插件还没释放,执行一次结束函数
if (!device.Driver.DisposedValue)
await device.FinishActionAsync().ConfigureAwait(false);
await device.FinishActionAsync();
}
}