由于单独的线程所有权,调用线程无法访问对象
Calling thread cannot access object due to separate thread ownership
所以我需要更新我的 WPF UI,这里是相同的代码。这会更新 UI 中的元素。
commandPrompt
对象是使用此处注明的第三方 UI 库创建的:http://www.codeproject.com/Articles/247280/WPF-Command-Prompt。我正在测试这个 UI 模型。
它抛出以下异常:
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));
}
}
}
这是 TextQuery 方法。这使用 RestSharp.dll
进行通信。
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));
}
}
}
}
}
这是用于写入同一自定义控制台的函数。
private void WriteToConsole(ConsoleWriteLineEventArgs e)
{
commandPrompt.OnConsoleWriteEvent(this, e); <=======EXCEPTION HERE
}
如何让线程在它们之间共享数据?我 Google 它但真的无法让它工作,因为我是异步编码的新手。
谢谢。
由于 WriteToConsole
调用 commandPrompt
,它访问 UI 元素,该调用应该在 UI 线程上进行。
由于您使用 Task
,您最终使用另一个线程从中调用 UI。这是不允许的,会给你你得到的错误。
您应该调用 Dispatcher.Invoke
或 BeginInvoke
在 UI 线程上进行调用:
Application.Current.Dispatcher.BeginInvoke
( new Action(() =>
WriteToConsole(new ConsoleWriteLineEventArgs(subPod.Title + "\n" + subPod.Plaintext)))
, DispatcherPriority.Background
);
所以我需要更新我的 WPF UI,这里是相同的代码。这会更新 UI 中的元素。
commandPrompt
对象是使用此处注明的第三方 UI 库创建的:http://www.codeproject.com/Articles/247280/WPF-Command-Prompt。我正在测试这个 UI 模型。
它抛出以下异常:
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));
}
}
}
这是 TextQuery 方法。这使用 RestSharp.dll
进行通信。
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));
}
}
}
}
}
这是用于写入同一自定义控制台的函数。
private void WriteToConsole(ConsoleWriteLineEventArgs e)
{
commandPrompt.OnConsoleWriteEvent(this, e); <=======EXCEPTION HERE
}
如何让线程在它们之间共享数据?我 Google 它但真的无法让它工作,因为我是异步编码的新手。
谢谢。
由于 WriteToConsole
调用 commandPrompt
,它访问 UI 元素,该调用应该在 UI 线程上进行。
由于您使用 Task
,您最终使用另一个线程从中调用 UI。这是不允许的,会给你你得到的错误。
您应该调用 Dispatcher.Invoke
或 BeginInvoke
在 UI 线程上进行调用:
Application.Current.Dispatcher.BeginInvoke
( new Action(() =>
WriteToConsole(new ConsoleWriteLineEventArgs(subPod.Title + "\n" + subPod.Plaintext)))
, DispatcherPriority.Background
);