Calling thread cannot access object due to separate thread ownership

Jishan

So I need to update my WPF UI and here is the code for the same. This updates and element in the UI. The commandPrompt object is created using a third party UI library noted here: http://www.codeproject.com/Articles/247280/WPF-Command-Prompt. I was testing this UI mockup. And it throws the exception below:

The calling thread cannot access this object because a different thread owns it.

 private void CommandProcess(object sender, ConsoleReadLineEventArgs EventArgs)
        {
            string[] command = new string[EventArgs.Commands.Length];

            for (int i = 0; i < EventArgs.Commands.Length; i++)
            {
                command[i] = EventArgs.Commands[i].ToLower();
            }
            if (command.Length > 0)
            {
                try
                {                    
                    switch (command[0])
                    {
                        case "clear":
                            ProcessClear(command);
                            break;
                        case "demo":
                            ProcessParagraph(command);
                            break;

                        default:
                            new Task(() => { TextQuery(command); }).Start();                         
                            break;
                    }
                }
                catch (Exception ex)
                {
                    WriteToConsole(new ConsoleWriteLineEventArgs("Console Error: \r" + ex.Message));
                }
            }
        }

This is the TextQuery method. This uses the RestSharp.dll to communicate.

private void TextQuery(string[] command)
    {
        string APIQuery = ConvertStringArrayToString(command);

        USBM usbm = new USBM("XXXXXX");

        QueryResult results = usbm.Query(APIQuery);
        if (results != null)
        {
            foreach (Pod pod in results.Pods)
            {
                Console.WriteLine(pod.Title);
                if (pod.SubPods != null)
                {
                    foreach (SubPod subPod in pod.SubPods)
                    {
                EXCEPTION?? ====>WriteToConsole(new ConsoleWriteLineEventArgs("" + subPod.Title + "\n" + subPod.Plaintext));

                    }
                }
            }
        }
    }

This is the function that is used to write to the same custom console.

private void WriteToConsole(ConsoleWriteLineEventArgs e)
{
            commandPrompt.OnConsoleWriteEvent(this, e); <=======EXCEPTION HERE
}

How can I let the threads share the data amongst them? I Google it but really couldn't get I it to work as I am new to async coding.

Thanks.

Patrick Hofman

Since WriteToConsole calls commandPrompt, which accesses UI elements, that call should be made on the UI thread.

Since you use Task, you end up using another thread to invoke the UI from. That is not allowed and will give you the error you get.

You should call Dispatcher.Invoke or BeginInvoke to make the call on the UI thread:

Application.Current.Dispatcher.BeginInvoke
( new Action(() => 
      WriteToConsole(new ConsoleWriteLineEventArgs(subPod.Title + "\n" + subPod.Plaintext)))
, DispatcherPriority.Background
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

The calling thread cannot access this object because a different thread owns it

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it (with Dispatcher)

From Dev

Famous the calling thread cannot access this object because a different issue

From Dev

Borrows and ownership of object on thread

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From Dev

ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

From Dev

Calling thread cannot access object when it's created from the same thread

From Dev

Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From Dev

Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

"calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

From Dev

The calling thread cannot access this object even after adding Dispatcher.Invoke

From Dev

WPF error 'calling thread cannot access this object' trying to play sounds in c# class

From Dev

Pass object to thread (access object outside thread)

From Dev

Calling a Method in a running Thread Object

From Dev

The thread cannot access an object because a different thread owns it when using wpf

From Dev

Cannot close form opened on a separate thread

From Java

How to access a Runnable object by Thread?

From Java

Recursive calling of run method due to Thread.currentThread().run()

From Dev

What are the different ways for calling my method on separate thread?

From Dev

Room: Cannot access database on the main thread *but*

From Dev

Android Room - Cannot access database on the main thread

From Dev

Access to an object of my SWT thread from my Swing thread

From Dev

Cannot find function removeLabel in object Gmail Thread

Related Related

  1. 1

    The calling thread cannot access this object because a different thread owns it

  2. 2

    the calling thread cannot access this object because a different thread owns it wpf

  3. 3

    The Calling thread cannot access this object because a different thread owns it,WPF

  4. 4

    WPF: The calling thread cannot access this object because a different thread owns it

  5. 5

    The calling thread cannot access this object because a different thread owns it (with Dispatcher)

  6. 6

    Famous the calling thread cannot access this object because a different issue

  7. 7

    Borrows and ownership of object on thread

  8. 8

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  9. 9

    ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

  10. 10

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

  11. 11

    Calling thread cannot access object when it's created from the same thread

  12. 12

    Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

  13. 13

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  14. 14

    Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

  15. 15

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  16. 16

    "calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

  17. 17

    The calling thread cannot access this object even after adding Dispatcher.Invoke

  18. 18

    WPF error 'calling thread cannot access this object' trying to play sounds in c# class

  19. 19

    Pass object to thread (access object outside thread)

  20. 20

    Calling a Method in a running Thread Object

  21. 21

    The thread cannot access an object because a different thread owns it when using wpf

  22. 22

    Cannot close form opened on a separate thread

  23. 23

    How to access a Runnable object by Thread?

  24. 24

    Recursive calling of run method due to Thread.currentThread().run()

  25. 25

    What are the different ways for calling my method on separate thread?

  26. 26

    Room: Cannot access database on the main thread *but*

  27. 27

    Android Room - Cannot access database on the main thread

  28. 28

    Access to an object of my SWT thread from my Swing thread

  29. 29

    Cannot find function removeLabel in object Gmail Thread

HotTag

Archive