I’ve got the following code (simplified) :
IEnumerable MyFunc(...){
IAsyncResult res = mSocket.BeginReceive(mReceptionArray, 0, pNbBytes, SocketFlags.None, null, null);
while (!res.IsCompleted){
yield return new WaitForFixedUpdate();
}
}
Which can be used like this :
foreach(object o in MyFunc());
Of course, the compiler complains because o is never used. Is there any way around that ?
EDIT
The foreach is executed in a coroutine. I need to block the execution of the coroutine until I receive data from a server. I could use a regular Receive(), but this would block all other coroutines, and I can’t do that : there are lots of other coroutines running, and blocking this one with Receive or EndReceive would block them.
Anyway, this is not a busy-wait.
Any suggestion to refactor the code is more than welcome, I’m not very experienced with C#.
I would write a new extension method to consume the sequence and ignore the results. For example:
It’s then clear in the calling code what you’re doing, and that you’re deliberately just consuming the sequence and ignoring the results.
Like sixlettervariables, it’s not really clear that your sequence is an appropriate one, but if you did just want to consume a sequence, that’s what I’d do.