The program needs to perform some operations in a separate stream, such as sending a request to the server and waiting for a response from it.
For implementation I use the Dataflow
library in order to do this all in a sequence.
Implemented this class:
partial class PoolManager { CancellationTokenSource CT; TransformBlock<Func<string>, string> ActionBlock; public PoolManager() { PM = this; CT = new CancellationTokenSource(); ActionBlock = new TransformBlock<Func<string>, string>(async n => { await Task.Delay(2000); string a = ""; try { a = n(); } catch (Exception e) { } return a; }, new ExecutionDataflowBlockOptions { CancellationToken = CT.Token }); } public void Add(Action func, string message) { ActionBlock.Post(new Func<string>(delegate () { func(); return message; })); } }
And the whole works as it should, but when I send a function to it, inside of which there is a sending and waiting for an answer, then at the time of waiting, the program interface hangs. On average, this is 100ms , sometimes it reaches 500ms or 1000ms , which becomes very noticeable. I do not understand what the reason is, for it should go in a separate stream.