c# - CheckForIllegalCrossThreadCalls in WPF -


i writing bot twitch , using library called twichlib (https://github.com/swiftyspiffy/twitchlib) in example made winforms there method called globalchatmessagereceived , there checkforillegalcrossthreadcalls = false;. whole method looks like

        private void globalchatmessagereceived(object sender, twitchchatclient.onmessagereceivedargs e)         {             //don't 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 unable checkforillegalcrossthreadcalls can point me on how should method solve crossthreadcalls?

the correct way use wpf dispatcher perform action on ui thread:

private void globalchatmessagereceived(object sender, twitchchatclient.onmessagereceivedargs e) {     var dispatcher = application.current.mainwindow.dispatcher;     // or use this.dispatcher if method in 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 can) don't need worry it.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -