BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TThread and Database connection

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Daryl
Guest





PostPosted: Sun May 06, 2007 6:50 am    Post subject: TThread and Database connection Reply with quote



Hi all
I am looking for ideas to write a NT Service that uses a TThread to watch a
folder. The thread will watch a folder and process a file when found, part
of this processing will involve looking up a database to get some
information based on a value found in the file.

My question what is the best way to handle this database connection?

Would I make the connection in the Main Thread or in the (in this case) the
only worker thread.? If I did create the connection in the Main Thread then
how could I pass this connection to the worker thread?

How would connection exceptions be managed? I was considering writing
exceptions to a file.

Any help would be good.

thanks
daryl
Back to top
JD
Guest





PostPosted: Sun May 06, 2007 8:10 am    Post subject: Re: TThread and Database connection Reply with quote



"Daryl" <devaccount (AT) hotmail (DOT) com> wrote:
Quote:

[...] Would I make the connection in the Main Thread

Yes.

Quote:
how could I pass this connection to the worker thread?

Don't.

Have the worker thread do the monitoring only and when it
detects a change, have it send a message to the main thread.

Quote:
How would connection exceptions be managed?

As usual ... in the main thread.

~ JD
Back to top
Daryl
Guest





PostPosted: Sun May 06, 2007 1:33 pm    Post subject: Re: TThread and Database connection Reply with quote



JD
Thanks fro the response, could you please help some more? I have placed
some questions in the text below.


Quote:
[...] Would I make the connection in the Main Thread

Yes.

To establish the connection to the database when the service first starts
may be an issue as the server, if local, may not be running? Any comments
on this?

Quote:
how could I pass this connection to the worker thread?

Don't.

Have the worker thread do the monitoring only and when it
detects a change, have it send a message to the main thread.

How would you send this message, an example would be good? I am guessing
that the filename would be passed, is that right?


Quote:
How would connection exceptions be managed?

As usual ... in the main thread.
Back to top
JD
Guest





PostPosted: Sun May 06, 2007 6:41 pm    Post subject: Re: TThread and Database connection Reply with quote

"Daryl" <devaccount (AT) hotmail (DOT) com> wrote:
Quote:

To establish the connection to the database when the service
first starts may be an issue as the server, if local, may
not be running?

Caution:

A Service should not have any GUI in it. While it's possible,
it's not reccommended and it's no longer possible with Vista.
Other than that, as for Services, I know very little.

Quote:
How would you send this message, an example would be good?
I am guessing that the filename would be passed, is that
right?

The thread could pass the file name but I would advise
against that because that's not how the Change Notification
works. You are only notified that a change has happened to
the folder. It's up to you to determine what the change was.
For example (untested):

//-------------------------------------------------------------
#ifndef MainH
#define MainH
//-------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//-------------------------------------------------------------
#include "MonitorThread.h"
//-------------------------------------------------------------
class TForm1 : public TForm
{
__published:
protected:
virtual void __fastcall WndProc(TMessage &Message);
private:
TMonitorThread *FMonitorThread;
public:
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
//-------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//-------------------------------------------------------------
#endif


//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
//-------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm( Owner )
{
FMonitorThread = new TMonitorThread( true, Handle );
FMonitorThread->SetWatchPath( "C:/SomePath" );
FMonitorThread->Resume();
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
FMonitorThread->Terminate();
FMonitorThread->WaitFor();
delete FMonitorThread;
}
//-------------------------------------------------------------
void __fastcall TForm1::WndProc( TMessage &Message )
{
if( Message.Msg == APP_FILECHANGE_NOTIFICATION )
{
ShowMessage( "Changed" );
}
else TForm::WndProc(Message);
}
//-------------------------------------------------------------



//-------------------------------------------------------------
#ifndef MonitorThreadH
#define MonitorThreadH
//-------------------------------------------------------------
#include <Classes.hpp>
//-------------------------------------------------------------
#define APP_FILECHANGE_NOTIFICATION (WM_USER + 100)
//-------------------------------------------------------------
class TMonitorThread : public TThread
{
private:
HWND FhWnd;
HANDLE FChangeHandle;
AnsiString FWatchPath;
DWORD dwStatus;
protected:
void __fastcall Execute();
public:
void __fastcall MonitorTerminate(TObject *Sender);
__fastcall TMonitorThread( bool, HWND, AnsiString );
};
//-------------------------------------------------------------
#endif


//-------------------------------------------------------------
//#include <vcl.h>
#pragma hdrstop

#include "MonitorThread.h"
//-------------------------------------------------------------
__fastcall TMonitorThread::TMonitorThread(bool CreateSuspended, HWND hWnd, AnsiString APath ) : TThread(CreateSuspended)
{
FChangeHandle = INVALID_HANDLE_VALUE;
FhWnd = hWnd;
FWatchPath = APath;
OnTerminate = MonitorTerminate;
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::MonitorTerminate(TObject *Sender)
{
if( FChangeHandle != INVALID_HANDLE_VALUE )
{
FindCloseChangeNotification( FChangeHandle );
}
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::Execute()
{
FChangeHandle = FindFirstChangeNotification( FWatchPath.c_str(), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE );

if( FChangeHandle != INVALID_HANDLE_VALUE )
{
while( !Terminated )
{
switch( WaitForSingleObject(FChangeHandle, 500) )
{
case WAIT_OBJECT_0: PostMessage( FhWnd, APP_FILECHANGE_NOTIFICATION, NULL, NULL );
if( ! FindNextChangeNotification(FChangeHandle) ) Terminate();
break;
case WAIT_TIMEOUT: break;
default : Terminate(); break;
}
}
FindCloseChangeNotification(FChangeHandle);
}
}
//-------------------------------------------------------------

Notice that I used PostMessage instead of SendMessage because
SendMessage will block the thread until you have completely
processed the message and SendMessage returns.

~ JD
Back to top
JD
Guest





PostPosted: Sun May 06, 2007 6:44 pm    Post subject: Re: TThread and Database connection Reply with quote

"JD" <nospam (AT) nospam (DOT) com> wrote:
Quote:

FMonitorThread = new TMonitorThread( true, Handle );
FMonitorThread->SetWatchPath( "C:/SomePath" );

FMonitorThread = new TMonitorThread( true, Handle, "C:/SomePath" );

~ JD
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon May 07, 2007 10:40 pm    Post subject: Re: TThread and Database connection Reply with quote

"Daryl" <devaccount (AT) hotmail (DOT) com> wrote in message
news:463d3483 (AT) newsgroups (DOT) borland.com...

Quote:
I am looking for ideas to write a NT Service that uses a TThread to
watch a folder. The thread will watch a folder and process a file
when
found

The OS has notifications available for that. Look at
FindFirst/NextChangeNotification() and SHChangeNotifyRegister().

Quote:
what is the best way to handle this database connection?

Unless your underlying database engine is thread-safe, then I suggest
you create a separate connection in each thread that needs to access
the database.

Quote:
Would I make the connection in the Main Thread

I would not suggest that, no. Especially since TService runs in its
own worker thread anyway, so there is not much happening in the main
thread.


Gambit
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon May 07, 2007 10:42 pm    Post subject: Re: TThread and Database connection Reply with quote

"JD" <nospam (AT) nospam (DOT) com> wrote in message
news:463d557f$1 (AT) newsgroups (DOT) borland.com...

Quote:
[...] Would I make the connection in the Main Thread

Yes.

Not in a service, you wouldn't.

Quote:
Have the worker thread do the monitoring only and when
it detects a change, have it send a message to the main thread.

There is nothing in the main thread to receive and process custom
messages. TService runs in a worker thread, not the main thread

Quote:
How would connection exceptions be managed?

As usual ... in the main thread.

Not for a service, no.


Gambit
Back to top
JD
Guest





PostPosted: Mon May 07, 2007 11:17 pm    Post subject: Re: TThread and Database connection Reply with quote

"Remy Lebeau \(TeamB\)" <no.spam (AT) no (DOT) spam.com> wrote:
Quote:

Not in a service, you wouldn't.

I missed that he was writing a Service until I read his reply.

~ JD
Back to top
Daryl
Guest





PostPosted: Wed May 09, 2007 3:05 am    Post subject: Re: TThread and Database connection Reply with quote

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:463f6498$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Daryl" <devaccount (AT) hotmail (DOT) com> wrote in message
news:463d3483 (AT) newsgroups (DOT) borland.com...

I am looking for ideas to write a NT Service that uses a TThread to
watch a folder. The thread will watch a folder and process a file
when
found

The OS has notifications available for that. Look at
FindFirst/NextChangeNotification() and SHChangeNotifyRegister().

OK, I understand I have this part working I think reasonably well. This
occurs in the single worker thread. One question - How would you suggest
that I set-up the folder to watch, folder to move files to, folder that
contains the log file, the server and database name? Would it be best to
have an INI file that is read by the TService thread at start up and then it
passes this information to the single worker thread so it can set up its
connections.

I suppose this leads to another question - if the database connection
information does not result in a successful connection how would this be
managed? I can see that it cold write this to a log file but is it possible
to shut down the thread?

Quote:
what is the best way to handle this database connection?

Unless your underlying database engine is thread-safe, then I suggest
you create a separate connection in each thread that needs to access
the database.

I plan to only have one thread that will watch the folder and write to the
file or NT log so I will create the DB connection in it.
One question -

Quote:
Would I make the connection in the Main Thread

I would not suggest that, no. Especially since TService runs in its
own worker thread anyway, so there is not much happening in the main
thread.

I was thinking that TService was the main thread and the TThread I create as
the worker, so there is no Main thread?
Back to top
Daryl
Guest





PostPosted: Sun May 13, 2007 8:10 am    Post subject: Re: TThread and Database connection Reply with quote

Did not quite know how to place this back into the system... hope this does
not contravene any rules.


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:463f6498$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Daryl" <devaccount (AT) hotmail (DOT) com> wrote in message
news:463d3483 (AT) newsgroups (DOT) borland.com...

I am looking for ideas to write a NT Service that uses a TThread to
watch a folder. The thread will watch a folder and process a file
when
found

The OS has notifications available for that. Look at
FindFirst/NextChangeNotification() and SHChangeNotifyRegister().

OK, I understand I have this part working I think reasonably well. This
occurs in the single worker thread. One question - How would you suggest
that I set-up the folder to watch, folder to move files to, folder that
contains the log file, the server and database name? Would it be best to
have an INI file that is read by the TService thread at start up and then it
passes this information to the single worker thread so it can set up its
connections.

I suppose this leads to another question - if the database connection
information does not result in a successful connection how would this be
managed? I can see that it cold write this to a log file but is it possible
to shut down the thread?

Quote:
what is the best way to handle this database connection?

Unless your underlying database engine is thread-safe, then I suggest
you create a separate connection in each thread that needs to access
the database.

I plan to only have one thread that will watch the folder and write to the
file or NT log so I will create the DB connection in it.
One question -

Quote:
Would I make the connection in the Main Thread

I would not suggest that, no. Especially since TService runs in its
own worker thread anyway, so there is not much happening in the main
thread.

I was thinking that TService was the main thread and the TThread I create as
the worker, so there is no Main thread?
Back to top
Daryl
Guest





PostPosted: Sun May 13, 2007 8:10 am    Post subject: Re: TThread and Database connection Reply with quote

Did not quite know how to place this back into the system... hope this does
not contravene any rules.


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:463f6498$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Daryl" <devaccount (AT) hotmail (DOT) com> wrote in message
news:463d3483 (AT) newsgroups (DOT) borland.com...

I am looking for ideas to write a NT Service that uses a TThread to
watch a folder. The thread will watch a folder and process a file
when
found

The OS has notifications available for that. Look at
FindFirst/NextChangeNotification() and SHChangeNotifyRegister().

OK, I understand I have this part working I think reasonably well. This
occurs in the single worker thread. One question - How would you suggest
that I set-up the folder to watch, folder to move files to, folder that
contains the log file, the server and database name? Would it be best to
have an INI file that is read by the TService thread at start up and then it
passes this information to the single worker thread so it can set up its
connections.

I suppose this leads to another question - if the database connection
information does not result in a successful connection how would this be
managed? I can see that it cold write this to a log file but is it possible
to shut down the thread?

Quote:
what is the best way to handle this database connection?

Unless your underlying database engine is thread-safe, then I suggest
you create a separate connection in each thread that needs to access
the database.

I plan to only have one thread that will watch the folder and write to the
file or NT log so I will create the DB connection in it.
One question -

Quote:
Would I make the connection in the Main Thread

I would not suggest that, no. Especially since TService runs in its
own worker thread anyway, so there is not much happening in the main
thread.

I was thinking that TService was the main thread and the TThread I create as
the worker, so there is no Main thread?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.