-2

I have a requirement with problem statement is like say, when an event gets triggered, I have to call a task completion source, using async and await. There will be multiple events occurring, and each time an event occurs this task completion has to be called. (Also there would be multiple tasks which would be occurring, and raising this task completion)

Say for e.g. there are geometric shapes and blank spaces corresponding to these shapes. When these object shapes are put in this space, an event should trigger, which raises task completion source.

So want to implement a program which uses Task Completion source, based on events occurred, Dependency Injection, System events / notifications as well to check the events completed or not.(for multiple events).

Hope I am clear with the requirements, but since the problem statement is little tricky as it involves a lot of the TPL Async concepts I tried my best to explain, hence gave analogy of the shapes game above.

Any helpful suggestions / links / recommendations would be great.

Thanks In Advance!!!..

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
Siddharth
  • 436
  • 2
  • 11
  • 29
  • 1
    But you forgot to mention (or it is not clear te me) what problem using a `TaskCompletionSource` will solve. This seems like an [X Y problem](http://xyproblem.info/) – Peter Bons Apr 02 '19 at 17:06
  • If I have multiple tasks using "With" or "ContinueWith" there will be lot of thread deadlocks taking place. Hence want to achieve a notification trigger, each time an event completes or take place. – Siddharth Apr 02 '19 at 17:09
  • 2
    Do you have *any* code to share so we have a starting point? – Peter Bons Apr 02 '19 at 17:10

1 Answers1

2

A task completion source can only be completed one time. For your case, this doesn't sound like the correct solution. Take a look at SemaphoreSlim, which supports async and is thread neutral. It will let you release a waiting thread(WaitAsync) every time an event occurs (A shape is put in a space).

https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netframework-4.7.2

Peter Sulucz
  • 116
  • 4
  • Hey thanks for the reply. But using "Wait" would be like a performance hit. Any ways to avoid it while using task parallel library?. – Siddharth Apr 03 '19 at 05:54
  • Yes, SemaphoreSlim has a WaitAsync method, which uses the TPL and won't block your threadpool threads. https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim.waitasync?view=netframework-4.7.2 – Peter Sulucz Apr 04 '19 at 23:06