0

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.

Jishan
  • 1,654
  • 4
  • 28
  • 62

1 Answers1

2

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
);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325