• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

c#: spawn a process from a service

can someone show me a piece of code that works in c# - spawning a process (createprocess / process api) from a service ?

I dont need to run GUI applications or anything that demands a user to be logged on to the system, i just need to be able to run small worker jobs (exe / cmd files).

its not a problem outside the service, but inside it seems to be difficult and there are tonnes of examples on the net and discussions about it, those ive tried hasnt worked yet.

So would be thankful if you could post a code that works - thanks
0
thordk
Asked:
thordk
  • 3
1 Solution
 
tcullerCommented:
I'm not sure whaty ou mean by "from a service". If you need to create and keep track of a process, the Process object in the System.Diagnostics namespace is more than ideal. If you just need to start a process, calling static method Start of Process will suffice, also allowing you to supply optional command-line arguments in the method call. For example:

System.Diagnostics.Process iexplore = 
     new System.Diagnostics.Process("iexplore.exe",
"http://www.experts-exchange.com");
iexplore.Start();
// Do whatever you need to do
iexplore.Kill();
// Ends the process immediately.
 
// OR
 
System.Diagnostics.Process.Start("iexplore.exe",
"http://www.experts-exchange.com");

Open in new window

0
 
thordkAuthor Commented:
By service i mean - a "Windows Service" - you know, an application running in the background.
0
 
thordkAuthor Commented:
that doesnt seem to work no. There is a difference running from a windows-service than an ordinary application.

btw should be able to run on XP & Vista ->
0
 
käµfm³d 👽Commented:
The code that tculler posted is the method of starting a process (and yes it will work inside of a service). The issue you are most likely having is that you are expecting to see the process running (e.g. a command window). This will not happen because services run in their own space and do not have a visible desktop.
0
 
thordkAuthor Commented:
yes sorry its working fine, was abit fast there as i thought it was similar to another example. But thats working just good. thanks
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now