   [C#] Sending messages between console applications
  Hi,I have a console application that does some kind of lengthy data processing, and I would like the user to be able to cancel that processing while not leaving the processed data in a corrupted or otherwise invalid state. There is no way to handle that with ctrl+c, is there?I though I could add a special capability to my application, like "process.exe /cancel", which would somehow signal the other running "process.exe" that it needs to stop, gracefully.Currently I achieved that with a mutex. Before processing each item I try to acquire a mutex. Failure to do so means I need to terminate the program. Is there a better way? I'd much rather like a more generic messaging system where I can say something else than just "cancel". What can you advise here?Thanks!   
Quote:There is no way to handle that with ctrl+c, is there?There is.#ifdef WIN32BOOL WINAPI CtrlCHandler(DWORD dwCtrlType){if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT || dwCtrlType == CTRL_CLOSE_EVENT|| dwCtrlType == CTRL_LOGOFF_EVENT || dwCtrlType == CTRL_SHUTDOWN_EVENT){Terminate(0);return TRUE;}return FALSE;}#elsevoid signal_handler(int sig){Terminate(0);}#endifvoid Terminate(int nExitCode){nGlobalExitCode = nExitCode;bProgramShouldKeepRunning = false;}int main(...){// Add a Ctrl+C signal handler, for abrupt termination#ifdef WIN32SetConsoleCtrlHandler(&CtrlCHandler, TRUE);#else // (Linux)signal(SIGINT, &signal_handler);signal(SIGHUP, &signal_handler);#endif...}Look into SetConsoleCtrlHandler in MSDN.   ;Quote:Original post by shurcoolThere is.Just to expand a little on what I said...You can have that Ctrl+C handler function set a volatile bool flag to false.Next, be sure to check periodically if that flag is true in your main data processing code. When it becomes false, you'll know you should terminate cleanly.   ;  Take a look at the Console.CancelKeyPress event to handle Ctrl+C gracefully.   ;Quote:Original post by jantaCurrently I achieved that with a mutex.Quote:Original post by mutexTake a look at the Console.CancelKeyPress event to handle Ctrl+C gracefully.Heh, the irony.Btw, I just realized the OP was asking for C#-specific help, and I gave C++ code there. But hey, the idea is similar enough, and mutex gave a C# equivalent function/event name.   ;  Hey thanks for the replies.So, I'll look into the Ctrl+C things but anyway, this program can also be GUI driven, and I'd like to have a "cancel" button, so I suppose my original question is still valid.I found something about MS MessageQueue but that seems quite heavyweight and does not seem to work on my (Workgroup-based) computer.I should easily be able to come up with my own file based implementation but I can't believe that wouldn't be reinventing the wheel somehow...[Edited by - janta on May 3, 2009 6:46:51 AM];  MSMQ needs to be installed on your computer for it to work, it's definitaly a bit heavy weight for this...The simplest solution would be a named event object. You can created a named event handle with this constructor. The first process to use that constructor will create a new event, the second one to use that constructor will open the same event. They can then use the event to signal between each other.   ;  I'd look into using WCF. It doesn't require IIS to be installed on the computer, and it allows you to register a webservice interface in your application. Your other application can then call the webservice to interact with the "host".   