Move Zeos database operations to background

I recently started to work on a database client using the incredible Zeos components. Unfortunately though bigger queries caused the application to freeze which is not very appealing to any end-user. Due to performance issues I was forced to use data-aware components, and passing DataSets or DataSources across threads causes nasty access violations.
Following the rule of multithreaded applications using databases each thread must have it's own connection, datasets, etc. Since data-aware components are on the VCL's thread mixing them (Form1.DBGrid.DataSource := Thread1.PublishedDataSource) will award us with access violation errors. Unfortunately passing only object references (pointers) result the same, so we have to find an other way to work together.
 
The idea is to leave all components on the form, asking the thread to call the query's .Open method. Which - fortunately - we can send as a reference. Not to modify our code too much we need exceptions to "break out" and the main program flow to be paused until the dataset is fully loaded. To achieve this I added some helper class procedures, which will do just the above.
 
I know that the code is not efficient nor elegant, but it's working and easy to implement. Feel free to use it and let me know how it can be improved :)
 
 
P.s.: the unit with small changes is now included in the official Zeos 7.3 testing package. For historic purposes I'll leave this one available but if you can, use ZMethodInThread instead!

Add a comment

HTML code is displayed as text and web addresses are automatically converted.

This post's comments feed