Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Marc
Thu, 25 Oct 2007 16:29
Open a Form in a TServiceApplication during ServiceStart
Author: Marc
Date: Thu, 25 Oct 2007 16:29
Date: Thu, 25 Oct 2007 16:29
111 lines
4764 bytes
4764 bytes
Hi all
Here we go:
1) I create a simple TServiceApplication using the designer in BCB6.
2) Then I add the two events ServiceStart and ServiceStop to respond to the
service manager.
3) I install the service, start it, stop it, no problems :-)
4) Now I create a blank form and display [MyForm->Show()] it in the
ServiceStart event.
5) Again I install the service, start it, stop it, no problems :-)
6) Now I add a Button to my form, no events behind.
5) Again I install the service, start it, stop it => it crashes with the
error EOSError, Code: 1400 Invalid Window Handle.
If I check with the debugger, it crashes when it tries to release my form at
the end of the application.
The same thing happens when I try to put a TMemo component and write
something to it.
I have written services before that open Forms in the OnExecute loop.
Is it impossible to display a form when the service starts ? All I want is a
Splash screen with some information about the startup process of the
service.
Actually everything works fine during the service, the info is displayed,
etc. But when the application closes (is closed by the service manager) it
crashes.
Any ideas ?
Rds
Marc
************
Here is my sample code...
//---------------------------------------------------------------------------
#ifndef ServiceH
#define ServiceH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <SvcMgr.hpp>
#include <vcl.h>
//---------------------------------------------------------------------------
class TTestService : public TService
{
__published: // Von der IDE verwaltete Komponenten
void __fastcall ServiceStart(TService *Sender, bool &Started);
void __fastcall ServiceStop(TService *Sender, bool &Stopped);
private: // Anwender-Deklarationen
public: // Anwender-Deklarationen
__fastcall TTestService(TComponent* Owner);
TServiceController __fastcall GetServiceController(void);
friend void __stdcall ServiceController(unsigned CtrlCode);
};
//---------------------------------------------------------------------------
extern PACKAGE TTestService *TestService;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include "Service.h"
#include "MyForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTestService *TestService;
//---------------------------------------------------------------------------
__fastcall TTestService::TTestService(TComponent* Owner)
: TService(Owner)
{
}
//---------------------------------------------------------------------------
TServiceController __fastcall TTestService::GetServiceController(void)
{
return (TServiceController) ServiceController;
}
//---------------------------------------------------------------------------
void __stdcall ServiceController(unsigned CtrlCode)
{
TestService->Controller(CtrlCode);
}
//---------------------------------------------------------------------------
void __fastcall TTestService::ServiceStart(TService *Sender, bool &Started)
{
TheForm->Show();
Started=true;
}
//---------------------------------------------------------------------------
void __fastcall TTestService::ServiceStop(TService *Sender, bool &Stopped)
{
Stopped=true;
}
//---------------------------------------------------------------------------
Re: Open a Form in a TServiceApplication during ServiceStart
Author: Bruce Salzman
Date: Thu, 25 Oct 2007 16:50
Date: Thu, 25 Oct 2007 16:50
30 lines
1873 bytes
1873 bytes
"Marc" <info@mawnet.com> wrote in message news:4720c4bf@newsgroups.borland.com... > Hi all > > Here we go: > > 1) I create a simple TServiceApplication using the designer in BCB6. > > 2) Then I add the two events ServiceStart and ServiceStop to respond to the > service manager. > > 3) I install the service, start it, stop it, no problems :-) > > 4) Now I create a blank form and display [MyForm->Show()] it in the > ServiceStart event. > > 5) Again I install the service, start it, stop it, no problems :-) > > 6) Now I add a Button to my form, no events behind. > > 5) Again I install the service, start it, stop it => it crashes with the > error EOSError, Code: 1400 Invalid Window Handle. > Does Auto-creation of Forms work in a service? I'd try creating the Form with new in the constructor. -- Bruce
Re: Open a Form in a TServiceApplication during ServiceStart
Author: Remy Lebeau (TeamB)
Date: Thu, 25 Oct 2007 16:57
Date: Thu, 25 Oct 2007 16:57
29 lines
2172 bytes
2172 bytes
"Marc" <info@mawnet.com> wrote in message news:4720c4bf@newsgroups.borland.com... > Now I create a blank form and display [MyForm->Show()] > it in the ServiceStart event. DO NOT EVER display a UI from a service!!! Services are background processes. They are not supposed to interact with the user directly, with the exception of simply MessageBox() dialogs when notifications are needed. In fact, UIs in services are strictly not allowed anymore starting with Vista. For a service to display a UI, the proper way to handle that (in any OS version) is to create a separate non-service application that runs after the user has logged in, and then have that application communicate with the service as needed using any of a number of IPC (inter-process communication) technologies that are available, such as sockets or named pipes. > If I check with the debugger, it crashes when it tries to release my form > at the end of the application. Another reason not to use UIs in services - TService is multi-threaded, and the VCL's UI components are not thread-safe. > I have written services before that open Forms in the OnExecute loop. You should never have been doing that in the first place. Gambit
Re: Open a Form in a TServiceApplication during ServiceStart
Author: Marc
Date: Thu, 25 Oct 2007 17:34
Date: Thu, 25 Oct 2007 17:34
49 lines
2663 bytes
2663 bytes
Hi Gambit Mmmh... not the answer I expected, but thanks anyway. I guess I tried to take a short cut and avoid having to have two applications and communicate through IPC. Rds Marc "Remy Lebeau (TeamB)" <no.spam@no.spam.com> schrieb im Newsbeitrag news:4720cb19$1@newsgroups.borland.com... > > "Marc" <info@mawnet.com> wrote in message > news:4720c4bf@newsgroups.borland.com... > >> Now I create a blank form and display [MyForm->Show()] >> it in the ServiceStart event. > > DO NOT EVER display a UI from a service!!! Services are background > processes. They are not supposed to interact with the user directly, with > the exception of simply MessageBox() dialogs when notifications are > needed. In fact, UIs in services are strictly not allowed anymore starting > with Vista. For a service to display a UI, the proper way to handle that > (in any OS version) is to create a separate non-service application that > runs after the user has logged in, and then have that application > communicate with the service as needed using any of a number of IPC > (inter-process communication) technologies that are available, such as > sockets or named pipes. > >> If I check with the debugger, it crashes when it tries to release my form >> at the end of the application. > > Another reason not to use UIs in services - TService is multi-threaded, > and the VCL's UI components are not thread-safe. > >> I have written services before that open Forms in the OnExecute loop. > > You should never have been doing that in the first place. > > > Gambit >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads