我要写两个小程序。
(1)是一个负责合其他机子通讯的程序,其在退出时要运行几句代码。我写在form——closing中了。调试没问题
(2)是一个和设备数据交换(串口)的程序,其在收到设备上传的关机或重启指令时,能够执行自己的关机和重启。在关机指令只能由程序2发出,但是我想让程序1在被杀掉之间能够最后form-closing中的语句。
如何处理?谢谢。
两个程序都是C#写的。

解决方案 »

  1.   

    对,都是winform的。
    关键是程序1结束时要把自己的IP释放掉,要不然会有问题。
    所以最好其能在被关掉时释放一下。
    而1收到关机命令后我只能把一些进程杀掉,然后通过执行API的关机指令就关机了,如何能通知2一下呢。
      

  2.   

    1的程序开一个端口监听,当端口收了退出命令后就正常退出,而不是被2程序Kill掉,
    2程序发送给1程序监听端口关闭命令,并监视1进程是否退出,如果退出关机也可以用进程间通信解决呀,邮件槽,共享内存等都可以呀
      

  3.   

    使用进程间通信不就可以了么?
    A进程收到关机命令,通知B进程关闭,等待B回复,收到B的关闭回复,A关机,如果等待超时,A关机
      

  4.   

    //这样吧,你把form_closing()放到你关机的API(或者其他什么方法)前面一句去;
    //form_closing()这个说到底也是个函数,当正常的来用就可以了
      

  5.   

    magicsnake(北极狐) 本人很菜,对你说的还不太会,正在学习.能略详细些吗?
    准备用Herolegend() ( )的方法试试.
      

  6.   

    好象没什么办法..  连EXCEPTION 也不行
      

  7.   

    我想了一个方法。不过不确定有效。给你个思路。首先,我帖一段msdnRes
    The TerminateProcess function is used to unconditionally cause a process to exit. Use it only in extreme circumstances. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess. TerminateProcess causes all threads within a process to terminate, and causes a process to exit, but DLLs attached to the process are not notified that the process is terminating. Terminating a process causes the following: All of the object handles opened by the process are closed. 
    All of the threads in the process terminate their execution. 
    The state of the process object becomes signaled, satisfying any threads that had been waiting for the process to terminate. 
    The states of all threads of the process become signaled, satisfying any threads that had been waiting for the threads to terminate. 
    The termination status of the process changes from STILL_ACTIVE to the exit value of the process. 
    Terminating a process does not cause child processes to be terminated. Terminating a process does not necessarily remove the process object from the system. A process object is deleted when the last handle to the process is closed. 
      

  8.   

    private void Form1_Close(object sender, EventArgs e)
            {
                Process down = new Process();
                down.StartInfo.FileName = "shutdown";
                down.StartInfo.Arguments="-s -t 60";
                down.StartInfo.UseShellExecute = false;
                down.StartInfo.RedirectStandardError = true;
                down.StartInfo.RedirectStandardInput = true;
                down.StartInfo.RedirectStandardOutput = true;
                down.Start();
            }
      

  9.   

    Terminating a process does not cause child processes to be terminated. 看到了么,呵呵。所以。你可以尝试这样,启动程序的main函数里,启动一个后台监视进程,传递自己的进程句柄给监视进程,监视进程工作就是检查对象句柄是否有效。因为进程被杀死后,子进程仍然没挂掉。检测进程挂了。就可以执行些操作。
      

  10.   

    用通讯的常用方法:
      A要传信息给C,必需先进行B的验证..B验证成功转交给C.验证不成功转交给A重复发送.
       不知道对你有用不
      

  11.   

    好象都有些问题,今天从新开个问题,是这个问题的后继,请大家帮忙
    http://community.csdn.net/Expert/topic/5654/5654574.xml?temp=.9189417揭贴给分,多谢大家帮忙
      

  12.   

    我觉得这应该用Remotng 的技术能解决吧,
    你的问题是在程序2调用关机命令时,因为因为程序1的进程被强行结束,所以无法执行form_closing中的语句。你只要在收到关机或重新启动的命令前,向程序1发个关闭的指令,在程序2中检查程序1关闭后,再关机或重新启动
      

  13.   

    有关程序2向程序1发送指令的代码如果需要,你可以通过CSDN发送消息给我