Grasshopper

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);
});
}

Views: 1178

Replies to This Discussion

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):

http://www.albahari.com/threading/

BTW: Use some big numbers and have fun with this

Attachments:

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).

Attachments:

I have wrote a parallel component!

You can take as a reference.

Attachments:

Parallel Code(c#)

Attachments:

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service