Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use a <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k(SYSTEM.COMPONENTMODEL.BACKGROUNDWORKER);k(TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION%3dV4.0%22);k(DevLang-CSHARP)&amp;rd=true" rel="noreferrer"><code>BackgroundWorker</code></a> for the threading, use <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k(SYSTEM.DIAGNOSTICS.PROCESS.WAITFOREXIT);k(TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION%3dV4.0%22);k(DevLang-CSHARP)&amp;rd=true" rel="noreferrer"><code>Process.WaitForExit()</code></a> to wait for the process to terminate until you stop your service.</p> <p>You're right that you should do some threading, doing lots of work in the <code>OnStart</code> may render errors about not starting correctly from Windows when starting the service.</p> <pre><code>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(); } </code></pre> <p><strong>Edit</strong> You may also want to move the <code>Process p</code> to a class member and stop the process in the <code>OnStop</code> to make sure that you can stop the service again if the exe goes haywire.</p> <pre><code>protected override void OnStop() { p.Kill(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload