How to run exe as window service using c#
You should look into Process.Start
. You might want to try something like this:
Process.Start("sc", String.Format("create \"{0}\" binPath=\"{1}\"", serviceName, binPath));
How to Run an exe from windows service and stop service when the exe process quits?
You can use a BackgroundWorker
for the threading, use Process.WaitForExit()
to wait for the process to terminate until you stop your service.
You're right that you should do some threading, doing lots of work in the OnStart
may render errors about not starting correctly from Windows when starting the service.
protected override void OnStart(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo("file.exe");
p.Start();
p.WaitForExit();
base.Stop();
}
Edit
You may also want to move the Process p
to a class member and stop the process in the OnStop
to make sure that you can stop the service again if the exe goes haywire.
protected override void OnStop()
{
p.Kill();
}
Run windows application from Windows service
This can be achieved simply by:
1)creating one console application.
2)Setup and deployment of Console application by making Output type as Windows Application from properties.
Code as follows:
static void Main(string[] args)
{
Timer t = new Timer(callback, null, 0, 60000);
Thread.Sleep(System.Threading.Timeout.Infinite);
}
// This method's signature must match the TimerCallback delegate
private static void callback(Object state)
{
try
{
bool isAppRunning = IsProcessOpen("APPName");
if (!isAppRunning)
{
Process p = new Process();
string strAppPath;
strAppPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\FolderName\AppName.exe";
System.Diagnostics.Process.Start(strAppPath);
}
}
catch
{ }
}
public static bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}
How to start an exe from a .NET Windows Service for updating the service
I did this exact thing - using a simpler (some might say kludgy) approach. The service:
- Produces a batch command,
- Downloads the new executables to a staging location,
- Starts a process: cmd.exe which, in turn, runs the batch script w/o waiting for it to complete, and then
- Immediately terminates itself.
The batch command:
- Pings 127.0.0.1 five times,
- Copies the executables to the final location and,
- Restarts the service.
Works like clockwork. The ping is a reliable 5 second delay - lets the service shutdown before copying the files.
Edit:
Just for completeness - I realized that by batch cmd is pinging 127.0.0.1 not 128.0.0.1 and so I edited this answer to reflect that. I suppose either works - but 128.0.0.1 pings timeout, where 127.0.0.1 resolves to "me". Since I'm only using it as a poor-man's delay, it serves the purpose either way.
Start an application or batch file using Windows Service
A quick solution will be is to create one exe(Monitor.exe) just to monitor if your app process is running. If not launch your application.
The single responsibility of Monitor.exe is to ensure your main app is up and running
Related Topics
Cannot Use Ref or Out Parameter in Lambda Expressions
Xaml Binding Not Working on Dependency Property
C# Open a New Form Then Close the Current Form
How to Check the Number of Bytes Consumed by a Structure
How to Count Duplicates in List with Linq
Why Does This Async Action Hang When I Try and Access the Result Property of My Task
Why Can't C# Infer Type from This Seemingly Simple, Obvious Case
When to Use Blockingcollection and When Concurrentbag Instead of List<T>
Why Is a Dictionary "Not Ordered"
Word Wrap for a Label in Windows Forms
Write a Well Designed Async/Non-Async API
Is There Any Async Equivalent of Process.Start
Execute Multiple Command Lines with the Same Process Using .Net