Reputation: 1077
Good afternoon.
I need to admit this is a weird work to do, but I need it... :-)
Today there's a C# console application (I will name it App1.exe) called by my colleagues, working in COBOL. When the App1.exe finish its work, closes itself, then COBOL programs can go ahead and read files generated from App1.
This App1 is opened and closed hundred times a day, and it's not so quick... Now I need to get business logic in an systray app, always ready, to answer quickly to requests, but I cannot force my colleagues to change their programs: they will ever call App1.exe, waiting while it closes. So I createad a single istance App2.exe (I cannot use system services) that do the work; so now I thinked about this solution: App1.exe simply calls App2.exe (passing command line args), then it remains in stand-by (using Process.Start(App2.exe) and Process.WaitForExit()). When App2.exe finish to work, kills App1.exe, so COBOL programs can detect work is done and continue reading files generated from App2.exe.
In this solution (quick and dirty way, I'm just trying...) there's a problem: App2.exe was called by App1.exe, so it appears that a called exe cannot kill his parent, isn't it?
There's a better/reccommended way to do this kind of work? I can use only C# 2.0.
Thank you guys, any idea is welcome.
Nando
Upvotes: 0
Views: 470
Reputation: 1077
I found an interesting project: WCF Killer.
This is a simple yet powerful implementation of client-server TCP stack; probably a good solution for my needs.
Bye, Nando
Upvotes: 0
Reputation: 1077
This is the code i use to call and kill process.
//App1.exe
var p = Process.Start("App2.exe", myargs);
p.WaitForExit();
//App2.exe
//do the work calling my App2.exe business logic plugin...
//Close calling exe; caller variable is fullpath of my App1.exe, like D:\myapp\app1.exe
Process[] pArry = Process.GetProcesses();
foreach (Process p in pArry)
{
string s = p.ProcessName;
s = s.ToLower();
if (s.CompareTo(caller) == 0)
{
p.Kill();
}
}
Really simple code, maybe too simple to work :-)
Thank you!
Upvotes: 0
Reputation: 41993
Create a manager/monitor app, which oversees the whole process. It creates a new child app on-demand (App1). App1 can then request that App2 is started, by asking the manager/monitor.
If App2 then wants App1 closed, it can ask the manager/monitor to do it.
This way you can debug, log, monitor, audit and put rules in place for when each app is started/stopped. Much better!
Upvotes: 0
Reputation: 941317
Use one of the interprocess communication mechanisms to have app1.exe talk to app2.exe. Sockets, named pipes, Remoting, WCF. Since you only need to pass a command line, a simple socket can get the job done. Just exit app1.exe when you get a message back from app2.exe that the job was done, no need to kill anything.
Upvotes: 2