views:

240

answers:

2

I have a computationally intensive task that users can perform using a Silverlight app. It is a very easy task to parallelize.

My problem is that the System.Environment.ProcessorCount is Security Critical, so I can't easily check that.

I don't want to just QueueUserWorkItem because I don't want to have more than the number of processors executing -- that won't help.

One workaround is described here: http://www.codeproject.com/KB/silverlight/multicore.aspx

But it's a bit hackish, and it'd be a bit of work to make it reasonably reliable for 4 or 8 thread systems.

What's the "right" way to solve this? Or does Silverlight just leave you out of luck here?

+1  A: 

II don't know that the codeproject detection mechanism is all that bad. If nothing else you wrap up that code into a method to make an initial stab at it. When a better detection method comes along you change the implementation to that one, but I understand the issue in that you are only guaranteed to know that there are at least 2 cores. This of course depending on the threading you want to do is less performant on great hardware.

the other yuck thing to do is to allow the user to adjust the number of threads they want run in your app like 7zip does. if you detect 2+ cores, you could then expose a spin control to let the user decide how many threads they want. clunkier than an auto-detect, but at least in the case of 4+ cores you have the hope that the code runs more optimally.

MikeJ
+3  A: 

The fact that this method is SecurityCritical instead of SecuritySafeCritical is likely an artifact of the fact that at one point the desktop version .NET Framework version demanded EnvironmentPermission to get at this data.

We've since relaxed the demand on the desktop side so it really does make sense for this to be SafeCritical on Silverlight. I've raised this as an issue we should fix in a future release of Siverlight and it looks like we'll be able to do this.

If you find more cases where there are public methods that feel like they should be SafeCritical instead of Critical in Silverlight, do file some feedback at Microsoft Connect. Real people do look at these issues and it's your best bet for letting us know about your pain points.

For now it looks like you'll have to go with the workaround provided but at some point we'll fix the security annotation here.

This has been fixed in Silverlight 4.

Matt Ellis