+ Reply to Thread
Results 1 to 7 of 7

Thread: Graphics Layer inaccessible by background thread

  1. #1
    Dipin Behl
    Join Date
    Sep 2011
    Posts
    4
    Points
    0
    Answers Provided
    0


    0

    Default Graphics Layer inaccessible by background thread

    Hi All,

    I am using ArcGis API for WPF in a desktop based application to show locations, route for the stops on a 2D Map.
    A background thread fetches the locations (along with lat-long) from a WCF service. Then these locations are displayed on the Map as TextSymbol(s) on a GraphicLayer.
    Then another background thread calls a WCF RESTfull resource to find route for the locations (stops). This call returns me a route which i am trying to display on the map using another GraphicLayer using 'Add' method of the 'Graphics' property of the layer. Whenever i try to do so, i get an exception 'The calling thread cannot access this object because a different thread owns it.'
    I have tried to use Dispatcher object for the screen, the map object, the graphics layer but none seems to work for me.

  2. #2
    Loren Cress
    Join Date
    Feb 2011
    Posts
    11
    Points
    3
    Answers Provided
    0


    0

    Default Re: Graphics Layer inaccessible by background thread

    Quote Originally Posted by dipinbehl View Post
    I have tried to use Dispatcher object for the screen, the map object, the graphics layer but none seems to work for me.
    Can you post the code that you are using to accomplish this? What error do you get when you attempt to use the Dispatcher?

  3. #3
    Apurva Goyal
    Join Date
    Mar 2010
    Posts
    29
    Points
    5
    Answers Provided
    1


    0

    Thumbs down Re: Graphics Layer inaccessible by background thread

    Quote Originally Posted by klugerama View Post
    Can you post the code that you are using to accomplish this? What error do you get when you attempt to use the Dispatcher?
    I am getting similar error on my WPF application. Here is the code I am using-

    BackgroundWorker worker = new BackgroundWorker();
    worker.WorkerSupportsCancellation = true;
    //this is where the long running process should go
    worker.DoWork += (o, ea) =>
    {
    ea.Result = ReadShapeFileAsGraphics(droppedFilePaths[0]);
    };
    worker.RunWorkerCompleted += (o, ea) =>
    {
    IEnumerable<Graphic> graphics = ea.Result as IEnumerable<Graphic>;
    Dispatcher.CurrentDispatcher.Invoke((System.Action)(() =>
    {
    if (graphics.Count() > 0)
    foreach (Graphic g in graphics)
    GraphicLayer.GraphicsSource = graphics;

    }));

    //work has completed. you can now interact with the UI
    IsBusy = false;
    };
    //set the IsBusy before you start the thread
    IsBusy = true;
    worker.RunWorkerAsync();

    Thanks

    Apurva

  4. #4
    Loren Cress
    Join Date
    Feb 2011
    Posts
    11
    Points
    3
    Answers Provided
    0


    0

    Default Re: Graphics Layer inaccessible by background thread

    There are two issues here:

    Code:
                                        if (graphics.Count() > 0)
                                            foreach (Graphic g in graphics)
                                                GraphicLayer.GraphicsSource = graphics;
    This should be:
    Code:
                                        if (graphics.Count() > 0)
                                            GraphicLayer.GraphicsSource = graphics;
    Secondly, and more importantly:

    Code:
                                Dispatcher.CurrentDispatcher.Invoke((System.Action)(() =>
    ...should be:

    Code:
                                Dispatcher.CurrentDispatcher.Invoke((System.Action)((graphics) =>

  5. #5
    Brian Locke
    Join Date
    Jul 2010
    Posts
    82
    Points
    0
    Answers Provided
    1


    0

    Default Re: Graphics Layer inaccessible by background thread

    Isn't the System.Action Anonymous and takes 0 args?

    would we have to create a new delegate with a Graphic set as one of the args....

    ??? I have been trying to get this to work for days now and still can't seem to get this thing going

  6. #6
    Loren Cress
    Join Date
    Feb 2011
    Posts
    11
    Points
    3
    Answers Provided
    0


    0

    Default Re: Graphics Layer inaccessible by background thread

    Quote Originally Posted by brysageek View Post
    Isn't the System.Action Anonymous and takes 0 args?

    would we have to create a new delegate with a Graphic set as one of the args....

    ??? I have been trying to get this to work for days now and still can't seem to get this thing going
    System.Action is anonymous in this example, but it can take an argument (or more than one, if needed; see the link), so it doesn't need to have a delegate.

  7. #7
    Mike Branscomb

    Join Date
    Feb 2010
    Posts
    882
    Points
    359
    Answers Provided
    59


    0

    Default Re: Graphics Layer inaccessible by background thread

    Hi,

    I haven't had a chance to read the whole thread here - but you might want to take a look at the synchronization context info in http://forums.arcgis.com/threads/604...indows-Service.

    Cheers

    Mike
    ArcGIS Runtime Product Engineer

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts