[英]Exception “Object of type 'System.Object[]' cannot be converted to type”
I'm a newbie for C#. 我是C#的新手。 I have a problem while updating textbox in WPF from thread that running in another class.
从另一个类中运行的线程更新WPF中的文本框时遇到问题。
My exception is: 我的例外是:
Object of type 'System.Object[]' cannot be converted to type
类型为'System.Object []'的对象不能转换为类型
Here is my code in form: 这是我的代码形式:
public void UpdateUserOnline(userOnlineClass message)
{
if(onLineTextbox.Dispatcher.CheckAccess())
{
// The calling thread owns the dispatcher, and hence the UI element
for (int i = 0; i < message.user.Length; i++)
{
// this.Dispatcher.Invoke(Action)
onLineTextbox.AppendText("•" + message.user + "\r\n");
onLineTextbox.AppendText(" ->" + message.status + "\r\n");
// txtDestination.Items.Add(message.user[i]);
}
}
else
{
// Invokation required
onLineTextbox.Dispatcher.Invoke(DispatcherPriority.Normal,new UpdateUserOnlineCallback(UpdateUserOnline), new Object[] { message } );
}
}
And this is my part of code from my Thread class: 这是我的Thread类中的代码的一部分:
thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
thrMessaging.Start();
private void ReceiveMessages()
{
Response = null;
while (chatClient.Connected)
{
if (strm.DataAvailable)
{
Response = (commandClass)reciveFormatter.Deserialize(strm);
processMessage(Response);
//procProcessMessageCallback(processMessage), new object[] { Response });
//ProcessMessageCallback proccess = new ProcessMessageCallback(processMessage(Response));
}
}
}
private void processMessage(commandClass message)
{
/*
if (message.money != null)
{
UpdateUserRanking((userRankingClass)message.money);
}
*/
if (message.online != null)
{
waitinglist.UpdateUserOnline((userOnlineClass)message.online);
}
}
onLineTextbox.Dispatcher.Invoke(
DispatcherPriority.Normal,
new UpdateUserOnlineCallback(UpdateUserOnline),
new Object[] { message } );
You're using the wrong overload method. 您使用了错误的重载方法。
You're using invoke(priority,delegate,object) - but with an object array. 您正在使用invoke(priority,delegate,object)-但具有对象数组。
if you want to pass an object array - swap your priority and delegate around, like so: 如果要传递对象数组,请交换优先级并进行委派,如下所示:
onLineTextbox.Dispatcher.Invoke(
new UpdateUserOnlineCallback(UpdateUserOnline),
DispatcherPriority.Normal,
new Object[] { message } );
The overload you're using is expecting an Object, rather than Object[] 您正在使用的过载期待的对象,而不是使用Object []
Source: http://msdn.microsoft.com/en-us/library/ms591596.aspx and http://msdn.microsoft.com/en-us/library/cc647499.aspx 来源: http : //msdn.microsoft.com/en-us/library/ms591596.aspx和http://msdn.microsoft.com/en-us/library/cc647499.aspx
The Dispatcher.Invoke overloads are a bit strange. Dispatcher.Invoke重载有点奇怪。 There is a single argument overload, and a multiple argument overload that takes the first argument in a separate parameter from the params array of remaining arguments.
有一个单参数重载,一个多参数重载使第一个参数与其余参数的params数组中的参数分开。 Use "message" as the third argument without wrapping it in an array.
使用“消息”作为第三个参数而不将其包装在数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.