[英]CheckForIllegalCrossThreadCalls in WPF
I am writing my Bot for Twitch and i am using library called TwichLib ( https://github.com/swiftyspiffy/TwitchLib ) now in example which is made for WinForms there is method called globalChatMessageReceived
and there is CheckForIllegalCrossThreadCalls = false;
我正在为Twitch编写Bot,并且我正在使用名为TwichLib( https://github.com/swiftyspiffy/TwitchLib )的库,该示例是为WinForms制作的,有一个名为
globalChatMessageReceived
方法,并且有CheckForIllegalCrossThreadCalls = false;
. 。 So whole method looks like
所以整个方法看起来像
private void globalChatMessageReceived(object sender, TwitchChatClient.OnMessageReceivedArgs e)
{
//Don't do this in production
CheckForIllegalCrossThreadCalls = false;
richTextBox1.Text = String.Format("#{0} {1}[isSub: {2}]: {3}", e.ChatMessage.Channel, e.ChatMessage.DisplayName, e.ChatMessage.Subscriber, e.ChatMessage.Message) +
"\n" + richTextBox1.Text;
}
Now in WPF you are already unable to do this CheckForIllegalCrossThreadCalls so can someone point me on how should i properly do this method to solve this CrossThreadCalls? 现在在WPF中您已经无法执行此CheckForIllegalCrossThreadCalls,所以有人可以指出我如何正确地使用此方法来解决此CrossThreadCalls吗?
The correct way to do this is to use the WPF dispatcher to perform the action on the UI thread: 正确的方法是使用WPF调度程序对UI线程执行操作:
private void globalChatMessageReceived(object sender, TwitchChatClient.OnMessageReceivedArgs e)
{
var dispatcher = Application.Current.MainWindow.Dispatcher;
// Or use this.Dispatcher if this method is in a window class.
dispatcher.BeginInvoke(new Action(() =>
{
richTextBox1.Text = String.Format("#{0} {1}[isSub: {2}]: {3}", e.ChatMessage.Channel, e.ChatMessage.DisplayName, e.ChatMessage.Subscriber, e.ChatMessage.Message) +
"\n" + richTextBox1.Text;
});
}
Or, better, use data binding (if you can) so you don't need to worry about it. 或者,最好使用数据绑定(如果可以的话),这样就不必担心它了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.