Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, August 24, 2012 8:49 PM | 1 vote
hi...
I'm trying to run calc.exe from the windows service .
for that I have write following code .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace WindowsService_start_calc
{
public partial class Service1 : ServiceBase
{
Thread th;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Thread th;
th = new Thread(start_calc);
th.Start();
}
protected override void OnStop()
{
}
static protected void start_calc()
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Windows\system32\calc.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
Thread.Sleep(10000);
}
}
}
When I install this service using installer and setup wizard ....service get installed and can be seen in the Services.Then I start service .
but It does not launches calculator . not even one time .
For information I want to test this service on windows 7,windows server 2008 and above .
actualy running calc.exe from windows servicei useless but I'm just testing .
What should I do to run .exe from windows service ?
All replies (4)
Friday, August 24, 2012 10:21 PM ✅Answered | 3 votes
actualy running calc.exe from windows servicei useless but I'm just testing .
What should I do to run .exe from windows service ?
You can't run calc.exe (successfully) from a windows service because it's a GUI application. The service runs without a desktop session, so you shouldn't try to launch applications that require a user interface to function.
If you make a console application, you can launch it using your code.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Saturday, August 25, 2012 10:43 AM ✅Answered
I create a new windows form project a put the code below into a single module and got it to work properly. There were some thing in the original code that didn't work so I made changes as needed.
There is really no reason to create a thread because a class is a seperate thread. A class will stay in memory just like a thread until it is destroyed and class will run background processes just like a thread. You can add events into the class which will run just like a thread. Both threads and Events are run off the Timer Tick.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.ServiceProcess;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalc_Click(object sender, EventArgs e) { try { WindowsService_start_calc.Service1 myService = new WindowsService_start_calc.Service1(); } catch (Exception) { MessageBox.Show("Error loading calculator."); } } }}namespace WindowsService_start_calc{ public partial class Service1 : ServiceBase { public static Process process; public Service1() { //InitializeComponent(); string[] args = { "1", "2" }; OnStart(args); } protected override void OnStart(string[] args) { start_calc(); } protected override void OnStop() { } static protected void start_calc() { process = new Process(); process.StartInfo.FileName = @"C:\Windows\system32\calc.exe"; process.StartInfo.CreateNoWindow = true; process.StartInfo.ErrorDialog = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; process.Start(); } }}
jdweng
Friday, August 24, 2012 9:16 PM
Try using cmd.exe to launch the executable. I don't think calc.exe uses standard input nor standard output so you won't be able to communicate with the process. YOu can find the complete list of options for cmd.exe by opening up a DOS window and typing cmd/?
process.StartInfo.FileName = @"C:\Windows\system32\cmd.exe /K C:\Windows\system32\calc.exe";
jdweng
Friday, August 24, 2012 10:36 PM
Here's the code I used on a Calc Button on one of my apps:
private void btnCalc_Click(object sender, EventArgs e)
{
try
{
Process.Start("calc");
}
catch (Exception)
{
MessageBox.Show("Error loading calculator.");
}
}