Is it possible to create a function that looks like this:
void ExecuteInterleave(Action a, Action b) {
...
}
that will automatically execute a
and b
interleavingly on the current thread? For example, a
would run for a few hundred milliseconds, then b
would run, repeat. It would block the current thread until both a
and b
have returned.
This question is purely from a theoretical standpoint, as I'm curious if it's possible.
Automatically - no since there is no way for current thread to magically stop executing a method.
If you can get delegates to cooperate in multitasking - doable. Cool looking approach would be to use yield return
in each delegate to and driver method would iterate both "sequences" in order it wants.
IEnumerable<int> A()
{
// cooperative step 1
yield return 0;
// cooperative step 3
yield return 0;
}
IEnumerable<int> B()
{
// cooperative step 2
yield return 0;
// cooperative step 4
yield return 0;
}
void ExecuteInterleave(Func<IEnumerable<int>> a, Func<IEnumerable<int>> b) {
var i1 = a().GetEnumerator();
var i2 = b().GetEnumerator();
i1.MoveNext();
i2.MoveNext();
i1.MoveNext();
i2.MoveNext();
}
...
ExecuteInterleave(A, B);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments