Below is the test code I used. As you can see, this is an infinite loop continuously reading data through ReadAsync.
for (; ;)
{
Logger logger = new Logger(nameof(OpcController), nameof(GetItemValues));
using var server = new OpcDaServer(_opcClientConfigSection.ProgId);
server.Connect();
using OpcDaGroup group = server.AddGroup(Guid.NewGuid().ToString("N"));
OpcDaItemDefinition[] definitions = items
.Select(x => new OpcDaItemDefinition
{
ItemId = x,
IsActive = true
})
.ToArray();
OpcDaItemResult[] results = group.AddItems(definitions);
OpcDaItemValue[] values = await group.ReadAsync(group.Items);
}
However, after running it, you can see through Task Manager that the Process Threads of the OPC Server keep increasing.

Then I found that changing await group.ReadAsync(group.Items) to group.ReadAsync(group.Items).Result can avoid the issue, but obviously, this is not the root cause. So I tried to find the real cause in the source code
|
private readonly TaskCompletionSource<OpcDaItemValue[]> _tcs = new TaskCompletionSource<OpcDaItemValue[]>(); |
and discovered that adding TaskCreationOptions.RunContinuationsAsynchronously to the following code can solve the problem.
private readonly TaskCompletionSource<OpcDaItemValue[]> _tcs = new TaskCompletionSource<OpcDaItemValue[]>(TaskCreationOptions.RunContinuationsAsynchronously);
Honestly, I am not familiar with Async and don't understand the underlying principle. I hope someone can help explain.
Below is the test code I used. As you can see, this is an infinite loop continuously reading data through ReadAsync.
However, after running it, you can see through Task Manager that the Process Threads of the OPC Server keep increasing.

Then I found that changing await
group.ReadAsync(group.Items)togroup.ReadAsync(group.Items).Resultcan avoid the issue, but obviously, this is not the root cause. So I tried to find the real cause in the source codeTitaniumAS.Opc.Client/TitaniumAS.Opc.Client/Da/Internal/Requests/ReadAsyncRequest.cs
Line 14 in 77e34b6
and discovered that adding
TaskCreationOptions.RunContinuationsAsynchronouslyto the following code can solve the problem.Honestly, I am not familiar with Async and don't understand the underlying principle. I hope someone can help explain.