本文介绍了关闭托管过程中最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动的过程B.
处理A他们在WCF(IAsyncResult的APM模式)进行通信,为客户B的服务。
当进程A死了,我需要关闭B.

我送过程一个ID为ARG处理b和他所做的:

  Process.GetProcessById(进程ID).WaitForExit();
 

这是工作的罚款。
我的问题是如何杀死进程B正确,如果过程中死亡?

我尝试了所有的以下内容:

  Process.GetCurrentProcess()关闭()。
Process.GetCurrentProcess()的Dispose()。
Application.Exit(); >它的一个guiless WinForm应用程序
 

这是工作的唯一的事是:

  Process.GetCurrentProcess()杀死()。
 

但是,这不是杀人的过程是太野蛮?

添加code:
里面主要方法B:

  TrackExternalProcess(externalProcessId,()=> {OnExternalProcessClosed();});
 

public方法:

 公共静态无效TrackExternalProcess(INT进程ID,动作onExternalProcessClosed)
    {
        新主题(新的ThreadStart(委托
        {
            尝试
            {
                Thread.CurrentThread.IsBackground = TRUE;
                Process.GetProcessById(进程ID).WaitForExit();

                logger.Error(主机进程意外退出);

                onExternalProcessClosed();
            }
            赶上(例外)
            {
            }
        }))。开始();
    }

    公共静态无效OnExternalProcessClosed()
    {
        Process.GetCurrentProcess()杀死()。

    }
 

解决方案

有没有清洁的版本,我只是叫我的Dispose方法。
它照顾的一切,如关闭WCF服务。
最后Process.GetCurrentProcess()杀死();

I have process A that starts process B.
they communicate in WCF (IAsyncResult APM Pattern), A is client B is service.
When Process A dies, i need to close B.

I am sending process A id as arg to process b and he does:

Process.GetProcessById(processId).WaitForExit();

This is working fine.
My question is how to kill process b properly if process a dies ?

I tried all the following:

Process.GetCurrentProcess().Close();
Process.GetCurrentProcess().Dispose();
Application.Exit(); > its a guiless winform application

The only thing that worked was:

Process.GetCurrentProcess().Kill();

But isnt killing the process is too brutal ?

Adding code:
Process B inside main:

TrackExternalProcess(externalProcessId, () => { OnExternalProcessClosed(); });

Public methods:

    public static void TrackExternalProcess(int processId, Action onExternalProcessClosed)
    {
        new Thread(new ThreadStart(delegate
        {
            try
            {
                Thread.CurrentThread.IsBackground = true;
                Process.GetProcessById(processId).WaitForExit();

                logger.Error("Host process quit unexpectedly");

                onExternalProcessClosed();
            }
            catch (Exception)
            {
            }
        })).Start();
    }

    public static void OnExternalProcessClosed()
    {
        Process.GetCurrentProcess().Kill();

    }
解决方案

There was no cleaner version, I simply called my Dispose method.
It took care for everything, e.g closed WCF services.
And finally Process.GetCurrentProcess().Kill();

这篇关于关闭托管过程中最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:06