Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-07

Program is busy

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Raivo Nael

When writing data to file or reading data stream directly from port or downloading file from internet then program hangs as long as it is busy and does not respond to any Windows messages. Also Window will never be refreshed before work is done. All you know that.

This is not serious problem. But what are ways to overcome this? Is using Windows timers for frequent short delays only solution? How this is done in commercial programs?

thaks for help,
Raivo

_________________________________________________________________ STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail

Reply by email

Lee, John

Raivo, you need to give a bit (or a lot!) more information re the version of fpc, the unit(s), and include the part of your program that's showing the fault if you want some suggestions...
Regards John

Earlier message 21 lines
-----Original Message----- From: Raivo Nael Sent: 18 July 2003 10:57 To: Subject: [fpc-pascal]Program is busy When writing data to file or reading data stream directly from port or downloading file from internet then program hangs as long as it is busy and does not respond to any Windows messages. Also Window will never be refreshed before work is done. All you know that. This is not serious problem. But what are ways to overcome this? Is using Windows timers for frequent short delays only solution? How this is done in commercial programs? thaks for help, Raivo This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.

Reply by email

Michał Kamburelis
to Raivo Nael: When writing data to file or reading data stream directly from port or downloading file…

If you're doing anything time-consumig (like reading data from internet socket) and you want your program to respond to user actions (to Windows / XWindows / any-other-event-driven-environment events), to repaint its windows etc. you have to merge event loop with your data-reading code.

Something like that:

procedure Read_Data_And_Process_Some_Events;
begin
   while (not end of data stream) do
   begin
     read some data
     process some events
   end;
end;

If you're reading from a file, "read some data" can be something like "read next 1000 bytes from a stream". BUT if you're reading from an internet socket you don't know how long it will take to get "next 1000 bytes" so you should rather limit your reading code by time. Something like

   time:=Now;
   while (Now-time < 5 seconds) do
   begin
     peek if there are some data in a stream, if there are - read them
   end;

However, that kind of loop is called busy-waiting because if the stream has no more data yet, your program will use a lot of processor time just for peeking. It can be solved by doing Sleep(<for some small amount of time>) after every unsuccesful peek (that's an easy but poor solution) or by doing time-limited waiting for data using OS functions, like select() on sockets (and that's a corect solution).

"process some events" under WinAPI should be something like
   if PeekMessage(msg, 0, 0, 0, PM_REMOVE) then
   begin
     if msg.message=WM_QUIT then break;
     TranslateMessage(msg);
     DispatchMessage(msg);
   end;

If you want only to repaint your windows you can ignore all events except WM_PAINT. In general, you should disable some menus/buttons etc. before calling Read_Data_And_Process_Some_Events. This should make things easier, and it will prevent Read_Data_And_Process_Some_Events from being invoked recursively.

Hope this helps,
Michalis Kamburelis

Reply by email