Pregunta

I have a .NET application using .NET Framework 3.5, written in C#. The application is invoking delegates asynchronously using BeginInvoke. The application works fine when compiled using Visual Studio 2012 RC, but it freezes when compiled with Visual Studio 2010 SP1 (the main thread is stuck in WaitOne waiting for async delegates to complete, but they never execute).

While it is possible the bug is in wrongly designed synchronization, what I am surprised is why it works in VS 2012 and does not work in VS 2010, while I am targeting the same framework version in both.

How is multi-targeting implemented in Visual Studio for .NET? Should one expect the executable behaviour to be identical when targeting the same framework version, or is there some part (compiler front end or some part of runtime libraries), which depends on the Visual Studio version used?

¿Fue útil?

Solución

Yes, there is a difference. The C# compiler used is different. See VS2008 targeting .NET 2.0 doesn't stop me from using C# 3 features. Note: This is different from how multi-targeting works with Visual C++, where the complete tool chain is used from old versions when targeting them.

The issue which hit me was a different lifetime scope for foreach variable. I was capturing the variable in a delegate, and while in VS 2012 each delegate captured its own version of the variable, in VS 2010 it did not.

VS 2012 is using C# 5 compiler, which implements the new behaviour. The change is documented in Visual C# Breaking Changes in Visual Studio 2012 RC. It is also described in What else is new in C# 5? or Lifting ForEach, Breaking Change in Visual Studio 2012.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top