本文介绍了更新文本框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 前一段时间我在写一个模拟控制台时得到了一些帮助。想法是编写一个包含文本框的VB应用程序,然后从应用程序中运行一个进程并将stdout重定向到文本框。我的问题是文本框有最大长度限制,所以我必须编写一个函数来删除一些文本,当它开始变满。这似乎有效,但是当文本框文本变长时,我遇到了奇怪的行为。当它达到大约22000个字符时,它突然停止更新,然后随机它在一分钟左右后再次开始更新。所以,如果来自控制台的数字已经编号,它就会是这样的。 2223 2223 2229 2229 ... 我错过了那些在文本框冻结的那一刻已经过去的行 它再次在33000左右做到了 - 最大值大约是100000.我感到很困惑,如果有人知道出了什么问题我会很感激他/她的帮助 谢谢 Tim。 解决方案 我推测它可能是垃圾收集器。听起来你的应用程序是执行大量字符串处理的,并且由于字符串是不可变的,所以丢弃的字符串对象的数量可以快速增长。 作为一个实验,我建议在应用程序中更频繁地调用GC.Collect() 当存在的垃圾很小且易于管理时。 这有两个原因,这不是一个好主意。每次垃圾收集器 运行来获取垃圾,它会挂起所有线程,所以只运行一个集合可能会导致你错过几个数字,如果它没有完成经常就够了。另一个原因是你通常不想要收集一个集合,因为GC优化了b $ b本身,并且强制过早收集通常更浪费处理器 资源而不是生产力。 但是,如果这些分钟长的失误在你运行应用程序时消失了,而b / b $ b更频繁的垃圾收集,那么这表明它是垃圾收集 这就是问题。 此时(如果是GC),你会想看看为什么应用程序正在创建这么多垃圾,并构建一个处理它的计划。 Derek Harmon 我会在开始调用GC.collect之前使用StringBuilder进行测试。 StringBuilder用于管理将被操作的字符串 经常因为String类的不变性。 当你调用GC.Collect时,你带来了将垃圾收集器作为你的应用程序放入相同的 线程中,而不是让它在自己的线程中工作。 在某些情况下,这实际上会减慢你的应用程序。下来。 " Derek Harmon" <螺******* @ msn.com>在消息中写道 news:%2 ****************** @ TK2MSFTNGP11.phx.gbl ... :: 应用程序正在执行大量的字符串处理,并且由于字符串是不可变的,所以数量的丢弃字符串对象可以快速增长。 作为一个实验,我建议在应用程序中更频繁地调用GC.Collect()时,周围的垃圾很小且易于管理。 有两个原因这不是一个好主意。每次垃圾收集器运行以获取垃圾邮件时,它会暂停所有线程,因此只运行集合可能会导致您错过一些数字,如果它没有经常完成。 你通常不想要一个集合的另一个原因是 GC自我优化,并且强制过早收集通常比生产成本更浪费处理器资源。 但是,如果运行带有更频繁垃圾收集的应用程序时这些分钟长的失误消失,那么这表明垃圾集合就是问题。 那时(如果它是GC),你会想看看为什么应用程序正在创造如此多的垃圾,并构建一个处理它的计划。 Derek Harmon HiA while ago I got some help in writing a mock console. The idea was to write a VB app that contained a textbox, then run a process from within the app and redirect the stdout to the textbox. My problem was that textboxes have maximum length limits, so I had to write a function to erase some of the text when it started getting full. That appears to work, but I am experiencing odd behaviour when the textbox text gets long. When it got to around 22000 characters, it suddenly stopped updating, and then randomly it started updating again after a minute or so. So, if the numbers coming from the console were numbered, it would have looked like this2223222322292229...I was missing those lines that had gone by in the minute where the textbox "froze"It did this again around 33000 - the max is around 100000. I am quite baffled and if anyone has an idea as to what is going wrong I would appreciate his/her helpThanksTim. 解决方案I speculate it might be the garbage collector. It sounds like your application isperforming a lot of string processing, and as strings are immutable the numberof discarded string objects can grow quickly.As an experiment, I''d suggest calling GC.Collect( ) more frequently in the appwhen the garbage that is laying around is small and manageable.There are two reasons this isn''t a good idea. Each time the garbage collectorruns to pick up the trash, it suspends all threads, so just running a collection maycause you to miss a few numbers if its not done frequently enough. Anotherreason you wouldn''t ordinarily want to call for a collection is that the GC optimizesitself, and forcing premature collections is generally more wasteful of processorresources than productive.However, if these minute-long lapses disappear when you run the application withmore frequent garbage collections, then that would suggest it is garbage collectionthat is the problem.At that point (if it is the GC), you''ll want to look at why the application is creating somuch garbage, and construct a plan for disposing of it.Derek Harmon : : I speculate it might be the garbage collector. It sounds like yourapplication is performing a lot of string processing, and as strings are immutable thenumber of discarded string objects can grow quickly. As an experiment, I''d suggest calling GC.Collect( ) more frequently in theapp when the garbage that is laying around is small and manageable. There are two reasons this isn''t a good idea. Each time the garbagecollector runs to pick up the trash, it suspends all threads, so just running acollection may cause you to miss a few numbers if its not done frequently enough.Another reason you wouldn''t ordinarily want to call for a collection is that theGC optimizes itself, and forcing premature collections is generally more wasteful ofprocessor resources than productive. However, if these minute-long lapses disappear when you run theapplication with more frequent garbage collections, then that would suggest it is garbagecollection that is the problem. At that point (if it is the GC), you''ll want to look at why theapplication is creating so much garbage, and construct a plan for disposing of it. Derek Harmon 这篇关于更新文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 23:12