1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using StackInjector . Settings ;
@@ -30,8 +31,17 @@ public bool AnyTaskLeft ()
3031 return this . tasks . Any ( ) ;
3132 }
3233
34+ public bool AnyTaskCompleted ( )
35+ {
36+ return this . tasks . Any ( t => t . IsCompleted ) ;
37+ }
38+
39+ // todo add a method to safely exit the await loop to be able to re-join later or maybe an Unloack() of some sort
40+ //! should check if exiting an await foreach loop and re-entering will not hack the control or lose data
3341 public async IAsyncEnumerable < T > Elaborated ( )
3442 {
43+ this . EnsureExclusiveExecution ( true ) ;
44+
3545 while ( ! this . cancelPendingTasksSource . IsCancellationRequested )
3646 {
3747 // avoid deadlocks
@@ -51,8 +61,28 @@ public async IAsyncEnumerable<T> Elaborated ()
5161 break ;
5262 }
5363 }
64+
65+ lock ( this . listAccessLock )
66+ this . exclusiveExecution = false ;
67+
68+ }
69+
70+ public Task Elaborate ( )
71+ {
72+ // must run syncronously
73+ this . EnsureExclusiveExecution ( ) ;
74+
75+ return
76+ Task . Run ( async ( ) =>
77+ {
78+
79+ await foreach ( var res in this . Elaborated ( ) )
80+ this . OnElaborated ? . Invoke ( res ) ;
81+ } ) ;
82+
5483 }
5584
85+
5686 // true if outher loop is to break
5787 private async Task < bool > OnNoTasksLeft ( )
5888 {
@@ -86,10 +116,18 @@ Task listAwaiter ()
86116 }
87117 }
88118
119+ private void EnsureExclusiveExecution ( bool set = false )
120+ {
121+ lock ( this . listAccessLock ) // reused lock
122+ {
123+ if ( this . exclusiveExecution )
124+ throw new InvalidOperationException ( ) ;
125+
126+ if ( set )
127+ this . exclusiveExecution = set ;
128+ }
129+ }
89130
90- public bool AnyTaskCompleted ( )
91- =>
92- this . tasks . Any ( t => t . IsCompleted ) ;
93131
94132
95133 }
0 commit comments