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
Post a Comment