André Botelho André Botelho - 1 month ago 16
C# Question

Pass parameters from a project to a specific class in another project

I just started to learn C# for a school project but I'm stuck on something.

I have a solution with 2 projects (and each project has a class), something like this:

Solution:


  • Server (project) (...) MyServerClass.cs, Program.cs

  • App (project) (...) MyAppClass.cs, Program.cs



In my "MyServerClass.cs", I have this:

class MyServerClass
{
...
public void SomeMethod()
{
Process.Start("App.exe", "MyAppClass");
}
}


How can I properly send, for example, an IP address and port? Would something like this work?

class MyServerClass
{
....
public void SomeMethod()
{
string ip = "127.0.0.1";
int port = 8888;
Process.Start("App.exe", "MyAppClass " + ip + " " + port);
}
}


Then in my "MyAppClass.cs", how can I receive that IP address and port?

EDIT:

The objective of this work is to practice processes/threads/sockets. The idea is having a server that receives emails and filter them, to know if they're spam or not. We got to have 4 or 5 filters. The idea was having them as separated projects (ex: Filter1.exe, Filter2.exe, ...), but I was trying to have only 1 project (ex: Filters.exe) and have the filters as classes (Filter1.cs, Filter2.cs, ...), and then create a new process for each different filter.

I guess I'll stick to a project for each filter!

Thanks!

Answer

There are a number of ways to achieve this, each with their own pros/cons.

Some possible solutions:

  • Pass the values in on the command line. Pros: Easy. Cons: Can only be passed in once on launch. Unidirectional (child process can't send info back). Doesn't scale well for complex structured data.
  • Create a webservice (either in the server or client). Connect to it and either pull/push the appropriate settings. Pros: Flexible, ongoing, potentially bi-directional with some form of polling and works if client/server are on different hosts. Cons: A little bit more complex, requires one app to be able to locate the web address of the other which is trivial locally and more involved over a network.
  • Use shared memory via a memory mapped file. This approach allows multiple processes to access the same chunk of memory. One process can write the required data and the others can read it. Pros: Efficient, bi-directional, can be disk-backed to persist state through restarts. Cons: Requires pointers and an understanding of how they work. Requires a little more manipulation of data to perform a read/write.

There are dozens more ways. Without knowing your situation in detail, it's hard to recommend one over another.

Comments