views:

69

answers:

2

Hello:

I have written a small program using Borland's C++ builder, and along the way, everything seemed fine. My program has a map window and a table window, and when a user presses a button, a long process is started that reads in all the map and table information and then displays that. Every time i ran it through the debugger, I had no issues. Then today, I decided to test it without running it through the debugger. To my horror, The program reads in the map information and then displays it on the paintbox canvas without a problem, but when it loads the information for the grid, the map gets erased!!! It appears to happen during the load phase for the table. this takes about 4 seconds, and during which time, the window tells me that it isnt responding. This is when the map gets erased. Anyone have any ideas on why this is happening? Its driving me nuts, and I dont really understand whats going on under the hood here.

UPDATE:
I have fixed the problem to some degree. I was poking around and found this: http://stackoverflow.com/questions/402832/avoiding-not-responding-label-in-windows-while-processing-lots-of-data-in-one/402871#402871
I added the code to run once in the middle of the data read in for the table. this fixed my problems. however, I was wondering if anyone knows why this is the case? why does my program going unresponsive cause my canvases to be erased?

+1  A: 

I have never used C++ Builder, but i used Delphi. I think the libraries are the same.

Does that component you use store the image data? It may only draw to the screen. Try covering the window of your app with another window. If it erases it, you have to use a component which stores the image.

See this, it is for Delphi, but it may help. There should be a Image component in C++ Builder. Try using that instead of PaintBox.

You can solve the unresponsivenes problem by running the time consuming task in a separate thread or calling some function that processes the window's messages.

Wikeno
+4  A: 

Marcus Junglas wrote a detailed explanation of the problem, which affects both Delphi and 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.

You can reduce the problem by calling Application->ProcessMessages(), while loading your map data, however I recomend using a separate thread to load the data.

stukelly
This is a generic Windows issue. If the any long processing takes place on the main messaging thread, the application will stop responding.
gbrandt