[Mono-list] Re: yield

Robert Jordan robertj at gmx.net
Tue Sep 13 15:07:48 EDT 2005


Hi Bernhard,

> 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):
> 
> Unhandled Exception: System.Exception: Trying to emit a local from a 
> different ILGenerator.

You should fill a bug report.

In the meanwhile something like that should work:

public static IEnumerable GetData()
{
    Monitor.Enter (lockit);
    try {
      for (int i = 0; i < 10; i++)
         yield return i;
    }
    finally {
      Monitor.Exit (lockit);
    }
}

Rob



More information about the Mono-list mailing list