locked
Parralel.For Causing Marshal Issues

    Question

  • Hi there,

    I'm trying to speed up parts of my code that can be done in parallel by using Parralel.For.  Unfortunately I'm experiencing marshal issues when attempting to instantiate an object...

    Windows.UI.Xaml.Media.PathFigure pFigFigure = new Windows.UI.Xaml.Media.PathFigure();
    

    System.Exception was unhandled by user code
      HResult=-2147417842
      Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
      Source=Windows.UI.Xaml.Media
      StackTrace:
           at Windows.UI.Xaml.Media.PathFigure..ctor()
           at InkCanvasPrototype.Utility.RenderUtility.<>c__DisplayClass2.<CreatePathListForStrokeFromLines>b__1(Int32 pIntCurSegment) in c:\Users\Nick\Documents\Visual Studio 11\Projects\InkCanvasPrototype\InkCanvasPrototype\Utility\RenderUtility.cs:line 58
           at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
      InnerException:

    Does this mean that only the main application thread can instantiate objects of this type?  I haven't tried invoking the calling thread yet as it would kind of defeat the purpose of me making the loop run in parallel.  Is there a way around this?

    Many thanks in advance.

    Nick.

    Wednesday, March 21, 2012 8:26 AM

Answers

All replies

  • Hi Nick, You should use ref new to create a new PathFigure class in your codes. Best regards, Jesse

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 5:49 AM
  • Hi Nick, You should use ref new to create a new PathFigure class in your codes. Best regards, Jesse

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Hey Jessie,

    I'm not sure I understand :( If I change to "ref new" I get a compiler error.  I've not seen this syntax in C# before, am I missing something?

    Thursday, March 22, 2012 8:08 AM
  • My bad! I seem to have ended up in the wrong group! Whoops!  I'll post it in the C# area sorry!
    Thursday, March 22, 2012 8:12 AM