编程语言
首页 > 编程语言> > c – 当我的程序“无响应”时,为什么我的paintBox Canvas被删除?

c – 当我的程序“无响应”时,为什么我的paintBox Canvas被删除?

作者:互联网

我用Borland的C builder编写了一个小程序,顺便说一句,一切似乎都很好.我的程序有一个地图窗口和一个表格窗口,当用户按下按钮时,会启动一个长进程,读取所有地图和表格信息,然后显示该信息.每次我通过调试器运行它,我都没有问题.然后今天,我决定测试它而不通过调试器运行它.令我恐惧的是,该程序读取地图信息,然后在没有问题的情况下将其显示在paintbox画布上,但是当它加载网格的信息时,地图会被删除!它似乎发生在表的加载阶段.这需要大约4秒钟,在此期间,窗口告诉我它没有响应.这是地图被删除的时候.任何人都有任何关于为什么会这样的想法?它让我疯狂,我真的不明白这里有什么.

更新:
我已经在某种程度上解决了这个问题.我在四处寻找并发现:Avoiding “(Not Responding)” label in windows while processing lots of data in one lump
我添加了代码,在读入数据的中间运行一次.这解决了我的问题.但是,我想知道是否有人知道为什么会这样?为什么我的程序没有响应导致我的画布被删除?

解决方法:

Marcus Junglas写了一个问题的detailed explanation,它影响了Delphi和C Builder.

When programming an event handler in
Delphi (like the OnClick event of a
TButton), there comes the time when
your application needs to be busy for
a while, e.g. the code needs to write
a big file or compress some data.

If you do that you’ll notice that your
application seems to be locked. Your
form cannot be moved anymore and the
buttons are showing no sign of life.
It seems to be crashed.

The reason is that a Delpi application
is single threaded. The code you are
writing represents just a bunch of
procedures which are called by
Delphi’s main thread whenever an event
occured. The rest of the time the main
thread is handling system messages and
other things like form and component
handling functions.

So, if you don’t finish your event
handling by doing some lengthy work,
you will prevent the application to
handle those messages.

您可以在加载地图数据时通过调用Application->ProcessMessages()来减少问题,但是我建议使用单独的线程来加载数据.

标签:c,windows,cbuilder
来源: https://codeday.me/bug/20190827/1739685.html