关于richtextbox的savefile函数的问题 [英] A question about richtextbox's savefile function

查看:183
本文介绍了关于richtextbox的savefile函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组件richtextbox名为rtbEditGcode,我使用rtbEditGcode_TextChanged检查richtextbox的内容是否已更改。如果内容已更改,我使用savefile函数存储内容,但有时会抛出ioexception。



我怎么能解决这个问题?



我尝试过的事情:



i不知道如何尝试,谢谢你的回答。

My component richtextbox named rtbEditGcode,i used rtbEditGcode_TextChanged to check if richtextbox's content is changed.if content is changed,i use savefile function to store content,but sometimes it throw ioexception.

how can i slove this question?

What I have tried:

i don't know how to tried,thanks for answer.

推荐答案

我最好的猜测 - 如果没有在这里运行你的代码,猜测就是它的全部可以是 - 是SaveFile方法是异步的:它启动一个线程来进行保存并立即返回。如果是这样,那么你的IOException将是因为该文件正在使用上次保存,因为TextChanged事件在当前事件结束后立即再次触发。



正如JayanaChatterjee所说, TextChanged事件是一个保存信息的好地方,因为如果用户是一个好的打字员,它可以很快改变!

我建议在TextChanged事件中设置一个IsDirty标志并签入定时器是一种更好的方法。将Timer设置为60000的间隔,并在保存时重置标志。通常,我建议一个线程来处理这个问题,但由于你不能在另一个线程上访问控件,这只是意味着无论如何都要将请求调回主线程以进行保存!
My best guess - and without running your code here a guess is all it can be - is that the SaveFile method is asynchronous: it kicks off a thread to do the saving and returns immediately. If so, then your IOException would be because the file is in use from the last save because the TextChanged event fired again immediately the current event ended.

As JayanaChatterjee says, the TextChanged event is a bad place to save info as it can change pretty quickly if the user is a good typist!
I'd suggest that a "IsDirty" flag set in the TextChanged event and checked in a Timer would be a better approach. Set the Timer to an interval of 60000 and reset the flag when you save. Normally, I'd suggest a thread to handle this, but since you can't access the Controls on a different thread, it would just mean Invoking the request back to the main thread anyway in order to do the save!


这篇关于关于richtextbox的savefile函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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