algorithmic modeling for Rhino
Hi,
I would like to use Parallel.For for this simple loop:
private void RunScript(List<Guid> guid, List<Plane> plane, Box box)
{
for(int i = 0; i < plane.Count; i++){
Rhino.Render.TextureMapping textureMapping = Rhino.Render.TextureMapping.CreateBoxMapping(plane[i], box.X, box.Y, box.Z, true);
Rhino.RhinoDoc.ActiveDoc.Objects.ModifyTextureMapping(guid[i], 1, textureMapping);
}
}
I tried this one, but it crashes rhino immediately.
Honestly I am new to multi-threading, can somebody tell why crash occurs and how to properly use Parallel.For in this case? Thank you :)
private void RunScript(List<Guid> guid, List<Plane> plane, Box box)
{
System.Threading.Tasks.Parallel.For(0, plane.Count, i => {
Rhino.Render.TextureMapping textureMapping = Rhino.Render.TextureMapping.CreateBoxMapping(plane[i], box.X, box.Y, box.Z, true);
Rhino.RhinoDoc.ActiveDoc.Objects.ModifyTextureMapping(guid[i], 1, textureMapping);
});
}
Tags:
Try this
private void RunScript(List guid, List plane, Box box)
{
var numbers = Enumerable.Range(0, plane.Count);
var result = numbers.AsParallel().AsOrdered();
foreach (var i in result)
{
Rhino.Render.TextureMapping textureMapping = Rhino.Render.TextureMapping.CreateBoxMapping(plane[i], box.X, box.Y, box.Z, true);
Rhino.RhinoDoc.ActiveDoc.Objects.ModifyTextureMapping(guid[i], 1, textureMapping);
}
Thanks, Could you explain what is happening here:
var result = numbers.AsParallel().AsOrdered();
And why foreach instead of Parallel.foreach?
Parallel.ForEach will block until all tasks are completed or the tasks are cancelled.
I think the method ModifyTextureMapping canot be cancled.so it will block until the task is completed
Could you give any good reference about multi-threading in general?
Master talks // stuff (BTW: // is not the Jack for all trades as most people believe):
BTW: Use some big numbers and have fun with this
Forgot to mention this as well:
https://www.packtpub.com/application-development/multithreading-c-c...
This is a great reference:
http://james-ramsden.com/multithreading-a-foreach-loop-in-a-grassho...
This has a "minor" syntax error (it would never work).
Try the attached.
BTW: Some day may I post a combo of ~50 // "real-life" cases that prove that things are not that easy ... especially in AEC because the time required to figure out a proper scheduler denies - in most of cases - the benefits. Kinda like the catalysts in fuel injected engines (target missed my some miles from day zero).
Parallel Code(c#)
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by