2

I have class named worker, I want to create new instance of this class in a new process.
But I want to be able to communicate with this class after it will be open in a new process and being able to send and receive data.

What I want to do is that in any call to worker() a new instance will be open in a new process so I can see alot of worker.exe in my task manager.

I've done in before with vb com wrapper but now I want to do this only in C# and without COM,
Can I do this in the most basic way?

Example to class:

public class worker
{
    public worker()
    {
        // Some code that should be open in a new process
    }

    public bool DoAction()
    {
        return true;
    }
}

Example to main program:

worker myWorker = new worker();//should be open in a new process
bool ret = myWorker.DoAction();
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • Can you explain why you want it to work this way? There might be simpler options to achieve the same result. – Maarten Sep 11 '12 at 08:04

2 Answers2

3

You could expose your actions in WCF endpoints. Then, from one process, start another process. Then you can connect to the endpoint that that process exposes to communicate with it.

Typically, this is what WCF Named Pipes are used for.

Taken from link:

[ServiceContract(Namespace = "http://example.com/Command")]
interface ICommandService {

    [OperationContract]
    string SendCommand(string action, string data);

}

class CommandClient {

    private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
    private static readonly string PipeName = "Command";
    private static readonly EndpointAddress ServiceAddress = new EndpointAddress(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", ServiceUri.OriginalString, PipeName));
    private static readonly ICommandService ServiceProxy = ChannelFactory<ICommandService>.CreateChannel(new NetNamedPipeBinding(), ServiceAddress);

    public static string Send(string action, string data) {
        return ServiceProxy.SendCommand(action, data);
    }
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class CommandService : ICommandService {
    public string SendCommand(string action, string data) {
        //handling incoming requests
    }
}
static class CommandServer {

    private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
    private static readonly string PipeName = "Command";

    private static CommandService _service = new CommandService();
    private static ServiceHost _host = null;

    public static void Start() {
        _host = new ServiceHost(_service, ServiceUri);
        _host.AddServiceEndpoint(typeof(ICommandService), new NetNamedPipeBinding(), PipeName);
        _host.Open();
    }

    public static void Stop() {
        if ((_host != null) && (_host.State != CommunicationState.Closed)) {
            _host.Close();
            _host = null;
        }
    }
}
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
armen.shimoon
  • 6,303
  • 24
  • 32
1

Can you not just have a worker application which you fire up and begins the DoAction() method. Then use any inter process communication methods like named pipes to communication between them.

This explains it well, Anonymous pipes as opposed to Named Pipes like I mentioned.

Anonymous pipes offer less functionality than named pipes, but also require less overhead. You can use anonymous pipes to make interprocess communication on a local computer easier. You cannot use anonymous pipes for communication over a network.

Science_Fiction
  • 3,403
  • 23
  • 27