|Advanced interactive service|
|Step 1. Interactive service creation.|
On this step the interactive service with one form will be created. It is not too difficult. The only thing is that the service application is multi-thread and VCL is not thread safe. Moreover the service thread is not the main thread of application so some efforts are necessary to allow the form be accessed from service.
One approach to this problem was considered earlier. It was based on idea of complete separation of form's VCL code by placing it into dll, compiled without run-time packages. Another approach will be used below. It idea is to avoid potential threading errors by placing all forms into main thread of application. It is possible because new method was added to the TNtServiceApplication class. This method is named "Synchronize" and is similar to TThread.Synchronize. The only difference is that the TThread.Synchronize can be called from the TThread only while TNtServiceApplication.Synchronize can be called from anywhere.
Well, let's create the interactive service with a form. Create the new SvCom service application, new SvCom service and one new form. Some property changes are necessary to the resulting application:
The latest modification to be done is to add the form creating and releasing code. The service's OnStart and OnStop event handlers are acceptable for this purpose. The code modification should look like shown below.
As you see the CreateServiceForm and ReleaseServiceForm are not called directly. The Synchronize method is used to call them so they will be really called in the main application thread. It removes the thread safety problem.
|<< | Index | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Conclusions | >>|
|Add your comment | Read comments|