Win Service

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

Hi All, I have written a win service MSMQListener. On the OnStart method I
call a queue worker class which basically goes and invokes the delegate with
the following signature

OnStart()
{
QWorker q=new QWorker();
q.Run();
}

class QWorker{

void Run()
{
this.msmq=new MessageQueue(path);
this.msmq.ReceiveCompleted += new
System.Messaging.ReceiveCompletedEventHandler(this.msmq_ReceiveCompleted);
}
private void msmq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs
e)
{

// do something

}



} // end of QWorker

I wanted to know what should I do on the OnStop method of the Service
Controller, because if I call GC.Collect on the stop method my service
crashes.
BTW If Don't do anything on the OnStop method everything is fine!!!!!

OnStop()
{
GC.Collect(); // Crashes
}

TIA
 
Vai,

Why are you calling GC.Collect in the first place? You should let the
runtime do this on its own unless you have a specific reason.
 
To interrupt or stop the service and free resources. So should there be any
other code on the OnStop method?

TIA

Nicholas Paldino said:
Vai,

Why are you calling GC.Collect in the first place? You should let the
runtime do this on its own unless you have a specific reason.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vai2000 said:
Hi All, I have written a win service MSMQListener. On the OnStart method I
call a queue worker class which basically goes and invokes the delegate with
the following signature

OnStart()
{
QWorker q=new QWorker();
q.Run();
}

class QWorker{

void Run()
{
this.msmq=new MessageQueue(path);
this.msmq.ReceiveCompleted += new
System.Messaging.ReceiveCompletedEventHandler(this.msmq_ReceiveCompleted);
}
private void msmq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs
e)
{

// do something

}



} // end of QWorker

I wanted to know what should I do on the OnStop method of the Service
Controller, because if I call GC.Collect on the stop method my service
crashes.
BTW If Don't do anything on the OnStop method everything is fine!!!!!

OnStop()
{
GC.Collect(); // Crashes
}

TIA

--
========
Regards
Vai
========
 
Vai2000,

The whole point of the GC is that you do not have to manage these
things. Unless you have a very specific reason (other than freeing
resources, which isn't really adequate), you should let the GC handle
itself. For what you are doing, I would recommend removing the call to
GC.Collect. The runtime will manage everything appropriately.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vai2000 said:
To interrupt or stop the service and free resources. So should there be any
other code on the OnStop method?

TIA

message news:[email protected]...
Vai,

Why are you calling GC.Collect in the first place? You should let the
runtime do this on its own unless you have a specific reason.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vai2000 said:
Hi All, I have written a win service MSMQListener. On the OnStart
method
I delegate
with
 
Are you sure everything is fine?
IMO this service doesn't do what you expected, because you did something
fundametaly wrong, that is executing service code in the OnStart method
while it should only initialize the service and return as soon as possible
(at least before the SCM times out (30 sec.)).

Willy.
 
All I do is Write to file inside the ReceiveComplete Delegate and do
msmq.BeginReceive();

TIA
 
Back
Top