user1504869
user1504869

Reputation: 19

C# application execution using remoting and windows service

I am trying to start an application (in this case, notepad) by using remoting and a windows service. For testing, I'm trying to get notepad to run. While I seem to establish a connection (under processes in task manager, notepad is running), I don't see the window show up. I have a client which is used to send a command to run by typing in a text box and clicking a button, and the service should be able to pick up the command. There's 3 parts to my code:

MyNewService:

namespace MyNewService
{
    public partial class MyNewService : ServiceBase
    {
        bool serviceOn = false;
        string command = String.Empty;

        HttpChannel ch = new HttpChannel(4002);


        public MyNewService()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {
            ChannelServices.RegisterChannel(ch);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Service.Test), "ServiceURI", WellKnownObjectMode.Singleton);
        }
    }
}

Service:

namespace Service
{
    public class Test:MarshalByRefObject
    {

        public Test()
        {

        }
        public void runCommand(string command)
        {
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = command;
            myProcess.Start();
        }
    }
}

Client

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Test t;
        string command;

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(typeof(Test), "http://remoteComp:4002/ServiceURI");
            t = new Test();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            command = textBox1.Text;
            t.runCommand(command);
        }
    }
}

I've written a console application in place of the Windows Service in order to test to see if the window shows up, and the window, indeed, does show up. But using a Windows Service, the process runs only in the background.

Any idea what's going on?

Upvotes: 1

Views: 1471

Answers (1)

Absolom
Absolom

Reputation: 1389

A windows service should not perform interactions with a user. See the response from Daniel Brückner to this post. Daniel gives 2 links to do it anyway but the articles themselves strongly recommend to avoid these techniques.

Upvotes: 1

Related Questions