To the main page...The list of my products...Some texts...Sample applications, tips, tricks...If you need support...


 
Interactive service
Step 4. Creating a service form

Apr 11, 2011. Important note. Starting from Windows Vista interactive services are prohibited by Microsoft. Service application can not show forms even if it is marked as Interactive. SvCom suggests a workaround that allows to easily communicate with user from service application. Read more about this opportunity


As you know the Delphi VCL is not thread-safe. It means that all forms of Delphi application should live in one thread. Usually it is a main thread of application. At the same time we intend to create a form that will live in the service thread, not in the main thread of application. It is possible due to trick that is described below. This trick is not limited by SvCom services only. You can use it to create Delphi forms that live in separate threads in regular (not service) applications.

Before we start I would like to discuss the idea of this trick.

Let's consider the Delphi dll compiled without packages and containing some forms. Such dll can show it`s form in ANY thread of application (but all forms of dll must be in one thread). It will be safe because the whole VCL code in this dll will live in one thread. So if you have a set of forms that should live in a separate thread then place them into one dll, compile it without packages and use! It will work and it will be thread-safe.

OK, after this introduction the implementation of service form is easy enough. Do following steps:
Create new DLL project and save it as SvComLib3.dpr.
Add a form to it and save it as fmService.pas. Set the name of form to ServiceForm.
Place any controls on this form. For example it can be a label with caption 'Hello from service !'.
Add a procedure to the form source. This procedure should create and show our form. The code of this procedure should look like this:













 
 

 

var
  ServiceForm: TServiceForm;

procedure ShowForm;

implementation

{$R *.DFM}

procedure ShowForm;
begin
    TServiceForm.Create(nil).Show;
end;

end.

Export the ShowForm procedure. To do it add the exports section to the library dpr file:











 
 

library SvComLib3;

uses
  SysUtils,
  Classes,
  fmService in 'fmService.pas' {ServiceForm};

exports ShowForm;

begin
end.

Now return to the service module source and add the OnStart event handler to it. This event handler should call the ShowForm method. Do not forget to import it from SvComLib3.dll. Necessary source changes are shown below:


 




 
 

 

procedure ShowForm; external 'SvComLib3.dll';

procedure TSampleService3.SampleService3Start(Sender: TNtService;
  var DoAction: Boolean);
begin
    ShowForm;
end;

end.

Save all changes and compile both service application and it`s dll. Now our interactive service is ready for tests. Let's continue...

<< | Index | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | >>


 
© 1998-2014 Alexey Dynnikov