更新文本框文本 [英] Updating Textbox Text

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

问题描述




前一段时间我在写一个模拟控制台时得到了一些帮助。想法是编写一个包含文本框的VB应用程序,然后从应用程序中运行一个进程并将stdout重定向到文本框。我的问题是文本框有最大长度限制,所以我必须编写一个函数来删除一些文本,当它开始变满。这似乎有效,但是当文本框文本变长时,我遇到了奇怪的行为。当它达到大约22000个字符时,它突然停止更新,然后随机它在一分钟左右后再次开始更新。所以,如果来自控制台的数字已经编号,它就会是这样的。

2223

2223

2229

2229

...

我错过了那些在文本框冻结的那一刻已经过去的行


它再次在33000左右做到了 - 最大值大约是100000.我感到很困惑,如果有人知道出了什么问题我会很感激他/她的帮助


谢谢

Tim。

解决方案

" Tim" <一个******* @ discussions.microsoft.com>在消息新闻中写道:CD ********************************** @ microsof t.com ...

当文本框文本变长时,我遇到了奇怪的行为。当它达到大约22000个字符时,它突然停止更新,然后随机地它在一分钟左右后再次开始更新。
::它在33000左右再次这样做[...]我感到非常困惑,如果有人对于出了什么问题有任何想法我会很感激他/她的帮助。




我推测它可能是垃圾收集器。听起来你的应用程序是执行大量字符串处理的
,并且由于字符串是不可变的,所以丢弃的字符串对象的数量可以快速增长。


作为一个实验,我建议在应用程序中更频繁地调用GC.Collect()

当存在的垃圾很小且易于管理时。


这有两个原因,这不是一个好主意。每次垃圾收集器

运行来获取垃圾,它会挂起所有线程,所以只运行一个集合可能会导致你错过几个数字,如果它没有完成经常就够了。另一个原因是你通常不想要收集一个集合,因为GC优化了b $ b本身,并且强制过早收集通常更浪费处理器

资源而不是生产力。


但是,如果这些分钟长的失误在你运行应用程序时消失了,而b / b $ b更频繁的垃圾收集,那么这表明它是垃圾收集

这就是问题。


此时(如果是GC),你会想看看为什么应用程序正在创建这么多垃圾,并构建一个处理它的计划。

Derek Harmon


< blockquote>我会在开始调用GC.collect之前使用StringBuilder进行测试。


StringBuilder用于管理将被操作的字符串

经常因为String类的不变性。


当你调用GC.Collect时,你带来了将垃圾收集器作为你的应用程序放入相同的

线程中,而不是让它在自己的线程中工作。

在某些情况下,这实际上会减慢你的应用程序。下来。

" Derek Harmon" <螺******* @ msn.com>在消息中写道

news:%2 ****************** @ TK2MSFTNGP11.phx.gbl ...

"添" <一个******* @ discussions.microsoft.com>写在留言
新闻:CD ********************************** @ microsof t.com。 ..

当文本框文本变长时,我遇到了奇怪的行为。当
得到大约22000个字符时,它突然停止更新,然后随机
它在一分钟左右后再次开始更新。


::

它在33000左右再次这样做[...]我感到非常困惑,如果有人b $ b知道出了什么问题,我会很感激他/她的帮助。


<我推测它可能是垃圾收集器。这听起来像你的



应用程序正在执行大量的字符串处理,并且由于字符串是不可变的,所以
数量的丢弃字符串对象可以快速增长。

作为一个实验,我建议在
应用程序中更频繁地调用GC.Collect()时,周围的垃圾很小且易于管理。

有两个原因这不是一个好主意。每次垃圾
收集器运行以获取垃圾邮件时,它会暂停所有线程,因此只运行
集合可能会导致您错过一些数字,如果它没有经常完成。
你通常不想要一个集合的另一个原因是
GC自我优化,并且强制过早收集通常比生产成本更浪费
处理器资源。

但是,如果运行带有更频繁垃圾收集的
应用程序时这些分钟长的失误消失,那么这表明垃圾
集合就是问题。

那时(如果它是GC),你会想看看为什么
应用程序正在创造如此多的垃圾,并构建一个处理它的计划。

Derek Harmon



您如何推荐我使用StringBuilder解决问题


谢谢

Tim


Hi

A 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 this
2223
2223
2229
2229
...
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 help

Thanks
Tim.

解决方案

"Tim" <an*******@discussions.microsoft.com> wrote in message news:CD**********************************@microsof t.com...

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. : : It did this again around 33000 [...] I am quite baffled and if anyone has an idea
as to what is going wrong I would appreciate his/her help.



I speculate it might be the garbage collector. It sounds like your application is
performing a lot of string processing, and as strings are immutable the number
of discarded string objects can grow quickly.

As an experiment, I''d suggest calling GC.Collect( ) more frequently in the app
when the garbage that is laying around is small and manageable.

There are two reasons this isn''t a good idea. Each time the garbage collector
runs to pick up the trash, it suspends all threads, so just running a collection 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 the GC optimizes
itself, and forcing premature collections is generally more wasteful of processor
resources than productive.

However, if these minute-long lapses disappear when you run the application with
more frequent garbage collections, then that would suggest it is garbage collection
that is the problem.

At that point (if it is the GC), you''ll want to look at why the application is creating so
much garbage, and construct a plan for disposing of it.
Derek Harmon


I would use a StringBuilder and test it before I started calling GC.collect.

The StringBuilder is designed for managing strings that will be manipulated
often because of the immutability of the String class.

When you call GC.Collect, you bring the garbage collector into the same
thread as your application, rather than letting it work in its own thread.
In some cases, this can actually slow your app. down.
"Derek Harmon" <lo*******@msn.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...

"Tim" <an*******@discussions.microsoft.com> wrote in message news:CD**********************************@microsof t.com...

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.


: :

It did this again around 33000 [...] I am quite baffled and if anyone has an idea as to what is going wrong I would appreciate his/her help.



I speculate it might be the garbage collector. It sounds like your


application is performing a lot of string processing, and as strings are immutable the number of discarded string objects can grow quickly.

As an experiment, I''d suggest calling GC.Collect( ) more frequently in the app when the garbage that is laying around is small and manageable.

There are two reasons this isn''t a good idea. Each time the garbage collector runs to pick up the trash, it suspends all threads, so just running a collection 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 the GC optimizes itself, and forcing premature collections is generally more wasteful of processor resources than productive.

However, if these minute-long lapses disappear when you run the application with more frequent garbage collections, then that would suggest it is garbage collection that is the problem.

At that point (if it is the GC), you''ll want to look at why the application is creating so much garbage, and construct a plan for disposing of it.
Derek Harmon



How would you recommend I use StringBuilder to fix the problem

Thank you
Tim


这篇关于更新文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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