Windows Services

M

mp

I start a service and I have the following code:

//This code is Service2
if(Directory.Exists(pathFinal) && Directory.Exists(PathIntermediate))

{

m_threadFinal = new Thread(new System.Threading.ThreadStart(BuildFile));

m_threadFinal.Start();


}



private void BuildFile()

{

//all code works here

//Then when I am done I want to stop my service and restart another
service

DoThe magic();

}



Private void DoTheMagic()

{

//want to stop service2 itself and restart service1

try

{



ServiceController scStop = new ServiceController("service2");

ServiceRunning=false;

scStop.Stop();

scStop.Close();

ServiceController sc1 = new ServiceController("service1");

if (sc1.Status.Equals(ServiceControllerStatus.Stopped))

{

sc1.Start();

}

}

catch(Exception ex)

{

System.Diagnostics.EventLog.WriteEntry("error" + ex.Message ,
System.Diagnostics.EventLogEntryType.Error);

}

}

I do a Net Stop "service1" which works fine
then I do a Net Start "service2" , actually these are in bat files.
This works if i set schedule and run them manually , but If i wait for it to
run the next day, service2 does not stop!!!!!!!!!!!!!!! so weired!!!! can
anyone help???

Thanks
 
K

Ken Cox [Microsoft MVP]

That's off topic for an ASP.NET newsgroup but I'd look at the permissions
for the account that is running the batch file when you aren't.

It probably isn't allowed to interact with the desktop or something. Better
to take this issue to microsoft.public.dotnet.framework.component_services .
 
S

Steve C. Orr [MVP, MCSD]

I agree with Ken that this is most likely a permissions issue.
What user account is this service running as? It likely doesn't have
necessary permissions to run the bat file or execute something within the
batch file.
You can test this theory by configuring the service to run under your user
account (temporarily) since you know that you have permission to execute the
batch file. If it still doesn't work then the problem most likely has to do
with desktop interaction, which services don't handle well because they
don't have a desktop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top