检测MSI何时完成 [英] Detect when MSI finishes

查看:81
本文介绍了检测MSI何时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,先生们,我有Windows App .net 2.0 + vs2005.我的表单启动了多个MSI(app1.msi,app2.msi).请尝试使用Process.Start启动MSI.

我需要知道何时检测MSI可执行文件已完成? Perphaps,我启动MSI并等待它完成.

Hi all, misters,

I have Windows App .net 2.0 + vs 2005. My form launch several MSI's (app1.msi, app2.msi).

I try launch MSI using Process.Start.

I need know when detect MSI executable has finished ? Perphaps, I launch MSI and wait it until finish.

可以做到这一点:

将进程视作Process = Process.Start("C:\ ......... blah.exe")
process.WaitForExit( )


在论坛中,Bart Read说(在C#论坛中):

In forums, Bart Read says (in C# forums):

一个潜在的问题是,如果进程从未完成(例如,如果挂起),则此版本的WaitForExit()将永远等待.显然,您希望运行的MSI不会发生这种情况,但是以防万一,可能值得考虑.另外,我完全没有考虑如果MSI退出并显示失败代码(非零退出代码)会发生什么情况. Process还实现了IDisposable,因此一旦完成对对象的调用,就应该调用Dispose().

此示例考虑了以下因素:

using ( Process process = Process.Start( "C:\.....blah.exe" ) )
{
    process.WaitForExit( 30000 ); // Give process 30 seconds to execute (for example)
                                  // - this is probably far too short for running your MSIs
    if ( process.HasExited )
    {
        if ( process.ExitCode == 0 ) // Success
        {
            HandleSuccess();
        }
        else
        {
            HandleFailure();
        }
    }
    else
    {
        try
        {
            // You might also want to kill the process, although with an MSI this is not a good idea because you're almost
            // certainly going to leave your system in an inconsistent state. Anyway, if you do want to kill the process:
            process.Kill();

            // Maybe display timeout message or throw exception indicating timeout.
        }
        catch ( InvalidOperationException ) // Thrown by Kill(); indicates process has already exited
        {
            // Same code as above reproduced here for illustrative purposes only (should be separate method).
            if ( process.ExitCode == 0 ) // Success
            {
                HandleSuccess();
            }
            else
            {
                HandleFailure();
            }
        }
        catch ( Win32Exception w32e ) // Thrown by Kill() if, e.g., process can't be terminated
        {
            // Do exception handling: e.g. display error to user, or wrap and rethrow
        }
    }
}

继续进行下去,但是,如果您将应用程序分发给大量用户,并且您不真正喜欢或信任using(像我一样),那么您可能需要执行以下操作,即更加万无一失(尽管有些人不喜欢这种惯用法):

Process process = null;
try
{
   
process = Process.Start( "C:\.....blah.exe" ); >关于此的任何好的解决方案?我认为最好使用另一个线程,但是对我来说,问题是,如果MSI挂起或失败,如何终止进程?

One potential problem is that this version of WaitForExit() will wait forever if the process never finishes, for example if it hangs. Obviously, you'd hope that wouldn't happen with the MSIs you're running, but just in case, it might be worth taking into account. Also I haven't at all considered what might happen if the MSI exits with a failure code (non-zero exit code). Process also implements IDisposable, so once you're done with the object you should call Dispose().

This sample takes these things into account:

using ( Process process = Process.Start( "C:\.....blah.exe" ) )
{
    process.WaitForExit( 30000 ); // Give process 30 seconds to execute (for example)
                                  // - this is probably far too short for running your MSIs
    if ( process.HasExited )
    {
        if ( process.ExitCode == 0 ) // Success
        {
            HandleSuccess();
        }
        else
        {
            HandleFailure();
        }
    }
    else
    {
        try
        {
            // You might also want to kill the process, although with an MSI this is not a good idea because you're almost
            // certainly going to leave your system in an inconsistent state. Anyway, if you do want to kill the process:
            process.Kill();

            // Maybe display timeout message or throw exception indicating timeout.
        }
        catch ( InvalidOperationException ) // Thrown by Kill(); indicates process has already exited
        {
            // Same code as above reproduced here for illustrative purposes only (should be separate method).
            if ( process.ExitCode == 0 ) // Success
            {
                HandleSuccess();
            }
            else
            {
                HandleFailure();
            }
        }
        catch ( Win32Exception w32e ) // Thrown by Kill() if, e.g., process can't be terminated
        {
            // Do exception handling: e.g. display error to user, or wrap and rethrow
        }
    }
}

That's probably enough to be going on with, however if you're distributing your application to a very large number of peope and you don't really like, or trust using (like me), then you might want to do the following, which is a little more foolproof (some people don't like this idiom though):

Process process = null;
try
{
   
process = Process.Start( "C:\.....blah.exe" );
    process.WaitForExit( 30000 ); // Give process 30 seconds to execute (for example)
                                  // - this is probably far too short for running your MSIs
    if ( process.HasExited )
    {
        if ( process.ExitCode == 0 ) // Success
        {
            HandleSuccess();
        }
        else
        {
            HandleFailure();
        }
    }
    else
    {
        try
        {
            // You might also want to kill the process, although with an MSI this is not a good idea because you're almost
            // certainly going to leave your system in an inconsistent state. Anyway, if you do want to kill the process:
            process.Kill();

            // Maybe display timeout message or throw exception indicating timeout.
        }
        catch ( InvalidOperationException ) // Thrown by Kill(); indicates process has already exited
        {
            // Same code as above reproduced here for illustrative purposes only (should be separate method).
            if ( process.ExitCode == 0 ) // Success
            {
                HandleSuccess();
            }
            else
            {
                HandleFailure();
            }
        }
        catch ( Win32Exception w32e ) // Thrown by Kill() if, e.g., process can't be terminated
        {
            // Do exception handling: e.g. display error to user, or wrap and rethrow
        }
    }

    process.Dispose(); // If this throws an exception you'll know about it; whereas if another exception is thrown
                       // elsewhere this idiom means that an exception thrown by Dispose() won't mask it.
    process = null;
}
finally
{
    if ( process != null )
    {
        try { process.Dispose(); } catch ( Exception ) {}
    }
}

Any good solution about it ? I think is better use another Thread, but for me the problem is how kill the process if MSI hangs or fails ??

请MVP或Microsoft开发人员,这是解决此问题的一个或多个更好的解决方案?

Please, MVPs or Microsoft developers, which is the better solution or solutions for this issue ???

我想做个好习惯!!!

I want to do good practice !!!


有关它的任何建议或良好的示例代码源??

在此先谢谢问候


Any suggestions or good sample code source about it??

Thanks in advance, greetings

推荐答案


Hello Alhambra,

感谢您的帖子!我建议在这里发布一个非常好的论坛来发布您的ASP问题: http://forums.asp.net/

谢谢!


这篇关于检测MSI何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆