+ Reply to Thread
Results 1 to 4 of 4

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
    9
    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
    28
    Points
    0
    Answers Provided
    0


    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
    9
    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) =>

+ 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