在线程内部登录Richtextbox [英] Logging in Richtextbox inside thread

查看:68
本文介绍了在线程内部登录Richtextbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

好的,这一定是最愚蠢的选择,但我只是不明白.

我有WPF用户界面,其中有richtextbox"myLog".我也时不时地获取数据,这可能需要记录.当我的事件调用我的Log方法时,我收到错误消息:

调用线程无法访问该对象,因为其他线程拥有它"

这是我的logmethod的简化的想法:

Hi all

Okay, this must be some kind of monkeytest, but I just dont get it.

I have WPF UI where I have richtextbox "myLog". Also I get data now and then which could need logging. When my event call my Log method I receive error:

"The calling thread cannot access this object because a different thread owns it"

And here is simplified idea of my logmethod:

//LogMsgType msgtype: Error/Incoming/Normal/Outgoing,Warning , string msg: message 
private void Log(LogMsgType msgtype, string msg)
        {
        myLog.AppendText(msg); //here error occurs 
        }



我真的很感谢您的建议:)



i really appreciate your advices :)

推荐答案

您需要在此处执行Invoke(),因为文本框与尝试更新文本框的线程不同.

这应该为您指明正确的方向:

http://www.codeproject.com/KB/threads/winformsthreading.asp [ ^ ]
You need to do an Invoke() here because the textbox is owned by a different thread than the one trying to update it.

This should point you in the right direction:

http://www.codeproject.com/KB/threads/winformsthreading.asp[^]


这篇关于在线程内部登录Richtextbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆