[Mono-list] yield

Miguel de Icaza miguel at ximian.com
Thu Sep 22 21:29:58 EDT 2005


Hello,

> I wanted to use yield (like Miguel did it in HttpApplication), but I need to 
> lock by data. Is this not allowed?
> 
> static object lockit = new Object();
> 
> public static IEnumerable GetData()
> {
>   lock (lockit)
>   {
>    for (int i = 0; i < 10; i++)
>     yield return true;
>   }
> }
> 
> I am getting this error (with Mono, it works with MS.Net):

This is a bug in the compiler.  But even with the bug fixed the code
above will not work in the way you think it would.

The lock will be taken the first time, and released on the first yield,
and will not be taken again when the method resumes execution (although
Monitor.Exit will be called repeated times).

Basically yield and lock dont mix.

Miguel


More information about the Mono-list mailing list