I am trying to show a screensaver, and not freeze the application while it is connecting to the database. Normal connections (for MSSQL via ADO) take about 300 ms, and this does not cause the main thread to show "not responding" to windows.
However, in the case of (a) a network error or (b) a configuration error (invalid SQL server name / instance SQL), the wait time is 60 seconds. This not only makes the application immune, but it’s almost impossible to show any error or message when it freezes. I could open the message before starting the connection, but there really is no solution when the main thread blocks for 60 seconds.
The solution is similar to moving the connection to a background thread. This will result in the following code:
the TThread class, which makes a background connection, and some SyncObj, such as TEvent, is used to send the signal back to the main thread.
The loop in the main thread with this code:
BackgroundThread.StartConnecting;
while not BackgroundThread.IsEventSignalled do begin
Application.ProcessMessages; // keep message pump alive.
end;
// continue startup (reports error if db connection failed)
Is this the right way? My vibrations include the following elements of the above solution:
and. I would call Application.ProcessMessages, which I consider an extreme smell of code (this may be a valid exception to this rule)
Q. I am introducing themes into the application launch and I am worried about introducing errors.
If someone has a reference implementation that is known to be free of race conditions that can perform a background connection with ADO and is known to be a safe approach, that would be really helpful. Otherwise, general advice or partial examples are good.